UG二次开发NXOpen-Python(十) 滚珠丝杠副五次抛物线回珠曲线设计
在滚珠丝杠副中,一般都在螺母上设置滚珠循环装置(即反向器),用以构成滚珠循环链,实现滚珠在工作时的往返运动,反向器不仅影响螺母结构,而且对滚珠丝杠副的传动效率、运转灵活性和平稳性、承载能力等都有影响,目前国内较为流行的是五次抛物线型回珠曲线,它在平面内的投影为五次抛物线,其表达式为y=Ax+Bx3+Cx5,该形式回珠曲线在反向回珠始末处冲击小,适用于高速传动提高效率和降低噪音。
对于滚珠丝杠副的设计者来说,在日常工作中,可能需要频繁设计该曲线,所以,为简化设计,利用NXOpen-Python对该过程进行二次开发,以节省时间
首先,利用Block UI Styler简单的设计一个界面,如下:
界面设计过程很简单,通过点击左边的类型就能在对话框中添加相应的类型,然后在右侧对其属性进行一些修改,就和用VS拖界面差不多,当然你要不想用Block UI Styler,用tkinter做个界面也行,使用Block UI Styler设计完毕后保存后会自动生成一个.py文件和一个.dlx文件,把这两个文件放在application文件夹下,alt+F8,选择刚刚那个.py文件执行,就会弹出对话框,当然这个时候这个对话框什么都不做,我们需要编辑刚刚那个.py文件使该对话框具有相应的功能。
1 #------------------------------------------------------------------------- 2 # Callback Name: dialogShown_cb 3 # This callback is executed just before the dialog launch. Thus any value set 4 # here will take precedence and dialog will be launched showing that value. 5 #------------------------------------------------------------------------- 6 def dialogShown_cb(self): 7 try: 8 # ---- Enter your callback code here ----- 9 if self.enum0.ValueAsString == '圆形返向器': 10 self.double04.Show = True 11 self.double05.Show = True 12 self.double06.Show = False 13 self.double07.Show = False 14 else: 15 self.double04.Show = False 16 self.double05.Show = True 17 self.double06.Show = True 18 self.double07.Show = True 19 except Exception as ex: 20 # ---- Enter your exception handling code here ----- 21 self.theUI.NXMessageBox.Show( 22 "Block Styler", NXOpen.NXMessageBox.DialogType.Error, str(ex))
这个函数在对话框出来之前调用,所以可以用来初始化界面和各个参数值,例如上面代码的目的是为了实现不同的反向器类型时显示不同的反向器参数
1 #------------------------------------------------------------------------- 2 # Callback Name: update_cb 3 #------------------------------------------------------------------------- 4 def update_cb(self, block): 5 try: 6 if block == self.enum0: 7 if self.enum0.ValueAsString == '圆形返向器': 8 self.double04.Show = True 9 self.double05.Show = True 10 self.double06.Show = False 11 self.double07.Show = False 12 else: 13 self.double04.Show = False 14 self.double05.Show = True 15 self.double06.Show = True 16 self.double07.Show = True 17 # ---- Enter your code here ----- 18 elif block == self.double04: 19 # ---- Enter your code here ----- 20 pass 21 elif block == self.double06: 22 # ---- Enter your code here ----- 23 pass 24 elif block == self.double07: 25 # ---- Enter your code here ----- 26 pass 27 elif block == self.double05: 28 # ---- Enter your code here ----- 29 pass 30 elif block == self.double0: 31 # ---- Enter your code here ----- 32 pass 33 elif block == self.double01: 34 # ---- Enter your code here ----- 35 pass 36 elif block == self.enum01: 37 # ---- Enter your code here ----- 38 pass 39 elif block == self.double03: 40 # ---- Enter your code here ----- 41 pass 42 elif block == self.double02: 43 # ---- Enter your code here ----- 44 pass 45 elif block == self.double08: 46 # ---- Enter your code here ----- 47 pass 48 elif block == self.double09: 49 # ---- Enter your code here ----- 50 pass 51 elif block == self.double010: 52 # ---- Enter your code here ----- 53 pass 54 55 except Exception as ex: 56 # ---- Enter your exception handling code here ----- 57 self.theUI.NXMessageBox.Show( 58 "Block Styler", NXOpen.NXMessageBox.DialogType.Error, str(ex)) 59 60 return 0
这个函数在你对对话框里各个元素进行更改的时候执行,例如上面代码的目的是为了实现在使用时切换不同的反向器类型时显示不同的反向器参数
1 def apply_cb(self): 2 errorCode = 0 3 try: 4 # ---- Enter your callback code here ----- 5 f = FXCurve_NXH(self.double02.Value, 6 self.double09.Value, 7 self.double010.Value, 8 self.double03.Value, 9 self.double0.Value, 10 self.double01.Value, 11 self.double08.Value) 12 if self.enum0.ValueAsString == '圆形返向器': 13 points = f.getPoints_1( 14 0.1, 15 self.double05.Value, 16 self.double04.Value, 17 'R' if self.enum01.ValueAsString == '右旋' else 'L') 18 # self.lw.Open() 19 # for p in points: 20 # self.lw.WriteLine(str(p)) 21 else: 22 points = f.getPoints_2( 23 0.1, 24 self.double05.Value, 25 self.double06.Value, 26 self.double07.Value, 27 'R' if self.enum01.ValueAsString == '右旋' else 'L') 28 #================================================================== 29 # 生成样条曲线 30 #================================================================== 31 studioSplineBuilderEx1 = self.workPart.Features.CreateStudioSplineBuilderEx( 32 NXOpen.NXObject.Null) 33 xf, _ = f.getPointF(self.double05.Value) 34 for p in points: 35 point = self.workPart.Points.CreatePoint( 36 NXOpen.Point3d(p[0], p[1], p[2])) 37 if abs(abs(p[0]) - xf) < 0.00001: 38 point.SetVisibility( 39 NXOpen.SmartObjectVisibilityOption.Visible) 40 # point.SetVisibility(NXOpen.SmartObjectVisibilityOption.Visible) 41 geometricConstraintData = studioSplineBuilderEx1.ConstraintManager.CreateGeometricConstraintData() 42 geometricConstraintData.Point = point 43 studioSplineBuilderEx1.ConstraintManager.Append( 44 geometricConstraintData) 45 studioSplineBuilderEx1.Commit() 46 studioSplineBuilderEx1.Destroy() 47 #================================================================== 48 # 生成样条曲线结束 49 #================================================================== 50 51 #================================================================== 52 # 生成螺纹 53 #================================================================== 54 helixBuilder1 = self.workPart.Features.CreateHelixBuilder( 55 NXOpen.Features.Helix.Null) 56 helixBuilder1.StartAngle.Value = 0.0 57 helixBuilder1.SizeLaw.Value.Value = self.double0.Value 58 helixBuilder1.PitchLaw.Value.Value = self.double01.Value 59 helixBuilder1.LengthMethod = NXOpen.Features.HelixBuilder.LengthMethods.Turns 60 helixBuilder1.Turns.RightHandSide = "4" 61 if self.enum01.ValueAsString == "右旋": 62 helixBuilder1.TurnDirection = NXOpen.Features.HelixBuilderTurnDirections.RightHand 63 else: 64 helixBuilder1.TurnDirection = NXOpen.Features.HelixBuilderTurnDirections.LeftHand 65 point3 = self.workPart.Points.CreatePoint( 66 NXOpen.Point3d(0.0, -2 * self.double01.Value, 0.0)) 67 direction1 = self.workPart.Directions.CreateDirection( 68 NXOpen.Point3d(0.0, 0.0, 0.0), 69 NXOpen.Vector3d( 70 0.0, 1.0, 0.0), 71 NXOpen.SmartObjectUpdateOption.WithinModeling) 72 direction2 = self.workPart.Directions.CreateDirection( 73 NXOpen.Point3d(0.0, 0.0, 0.0), 74 NXOpen.Vector3d( 75 0.0, 0.0, 1.0), 76 NXOpen.SmartObjectUpdateOption.WithinModeling) 77 xform1 = self.workPart.Xforms.CreateXformByPointXDirZDir( 78 point3, direction2, direction1, NXOpen.SmartObject.UpdateOption.WithinModeling, 1.0) 79 cartesianCoordinateSystem1 = self.workPart.CoordinateSystems.CreateCoordinateSystem( 80 xform1, NXOpen.SmartObject.UpdateOption.WithinModeling) 81 helixBuilder1.CoordinateSystem = cartesianCoordinateSystem1 82 helixBuilder1.Commit() 83 helixBuilder1.Destroy() 84 #================================================================== 85 # 生成螺纹结束 86 #================================================================== 87 88 #================================================================== 89 # 生成返向器轮廓线 90 #================================================================== 91 matrix = NXOpen.Matrix3x3( 92 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0) 93 nm = self.workPart.NXMatrices.Create(matrix) 94 curves: NXOpen.CurveCollection = self.workPart.Curves 95 # 圆形返向器轮廓线 96 if self.enum0.ValueAsString == '圆形返向器': 97 arc = curves.CreateArc(NXOpen.Point3d(0.0, 0.0, 0.0), 98 nm, 99 self.double04.Value / 2, 100 0.0, 101 math.pi * 2) 102 arc.SetVisibility( 103 NXOpen.SmartObjectVisibilityOption.Visible) 104 # 扁圆形返向器轮廓线 105 else: 106 if self.enum01.ValueAsString == '右旋': 107 fxj = f.getPhi0(self.double05.Value) 108 # self.lw.Open() 109 # self.lw.WriteLine(str(fxj)) 110 l = self.double06.Value / 2 - self.double07.Value / 2 111 # self.lw.WriteLine(str(l)) 112 arc0 = curves.CreateArc(NXOpen.Point3d(-l * math.sin(fxj * math.pi / 180), -l * math.cos(fxj * math.pi / 180), 0.0), 113 nm, 114 self.double07.Value / 2, 115 0.0, 116 math.pi * 2) 117 arc0.SetVisibility( 118 NXOpen.SmartObjectVisibilityOption.Visible) 119 arc1 = curves.CreateArc(NXOpen.Point3d(l * math.sin(fxj * math.pi / 180), l * math.cos(fxj * math.pi / 180), 0.0), 120 nm, 121 self.double07.Value / 2, 122 0.0, 123 math.pi * 2) 124 arc1.SetVisibility( 125 NXOpen.SmartObjectVisibilityOption.Visible) 126 associativeLineBuilder1 = self.workPart.BaseFeatures.CreateAssociativeLineBuilder( 127 NXOpen.Features.AssociativeLine.Null) 128 associativeLineBuilder1.StartPointOptions = NXOpen.Features.AssociativeLineBuilder.StartOption.Tangent 129 associativeLineBuilder1.EndPointOptions = NXOpen.Features.AssociativeLineBuilder.EndOption.Tangent 130 associativeLineBuilder1.StartTangent.Value = arc0 131 associativeLineBuilder1.EndTangent.Value = arc1 132 associativeLineBuilder1.StartTangent.SetValue( 133 arc0, 134 NXOpen.View.Null, 135 NXOpen.Point3d( 136 -l * math.sin(fxj * math.pi / 180) + 137 self.double07.Value / 2 * 138 math.cos(60 * math.pi / 180), 139 -l * math.cos(fxj * math.pi / 180) + self.double07.Value / 2 * 140 math.sin(60 * math.pi / 180), 141 0.0)) 142 associativeLineBuilder1.EndTangent.SetValue( 143 arc1, NXOpen.View.Null, NXOpen.Point3d( 144 l * math.sin(fxj * math.pi / 180) + 145 self.double07.Value / 2 * 146 math.cos(60 * math.pi / 180), 147 l * math.cos(fxj * math.pi / 180) + self.double07.Value / 2 * 148 math.sin(60 * math.pi / 180), 149 0.0)) 150 151 l1 = associativeLineBuilder1.Commit() 152 associativeLineBuilder1.Destroy() 153 154 associativeLineBuilder1 = self.workPart.BaseFeatures.CreateAssociativeLineBuilder( 155 NXOpen.Features.AssociativeLine.Null) 156 associativeLineBuilder1.StartPointOptions = NXOpen.Features.AssociativeLineBuilder.StartOption.Tangent 157 associativeLineBuilder1.EndPointOptions = NXOpen.Features.AssociativeLineBuilder.EndOption.Tangent 158 associativeLineBuilder1.StartTangent.Value = arc0 159 associativeLineBuilder1.EndTangent.Value = arc1 160 associativeLineBuilder1.StartTangent.SetValue( 161 arc0, 162 NXOpen.View.Null, 163 NXOpen.Point3d( 164 -l * math.sin(fxj * math.pi / 180) + 165 self.double07.Value / 2 * 166 math.cos(240 * math.pi / 180), 167 -l * math.cos(fxj * math.pi / 180) + self.double07.Value / 2 * 168 math.sin(240 * math.pi / 180), 169 0.0)) 170 associativeLineBuilder1.EndTangent.SetValue( 171 arc1, NXOpen.View.Null, NXOpen.Point3d( 172 l * math.sin(fxj * math.pi / 180) + 173 self.double07.Value / 2 * 174 math.cos(240 * math.pi / 180), 175 l * math.cos(fxj * math.pi / 180) + self.double07.Value / 2 * 176 math.sin(240 * math.pi / 180), 177 0.0)) 178 179 l2 = associativeLineBuilder1.Commit() 180 associativeLineBuilder1.Destroy() 181 182 trimCurve2Builder1 = self.workPart.Features.CreateTrimCurve2FeatureBuilder( 183 NXOpen.Features.TrimCurve2.Null) 184 trimCurve2Builder1.KeepOrDiscard = NXOpen.Features.TrimCurve2Builder.KeepDiscard.Discard 185 trimCurve2Builder1.CurveOptions.InputCurveOption = NXOpen.GeometricUtilities.CurveOptions.InputCurve.Blank 186 trimCurve2Builder1.MakeInputCurvesDashed = False 187 trimCurve2Builder1.CurveExtensionOption = NXOpen.Features.TrimCurve2Builder.CurveExtension.Natural 188 189 curves1 = [arc0] 190 curveDumbRule1 = self. workPart.ScRuleFactory.CreateRuleBaseCurveDumb( 191 curves1) 192 trimCurve2Builder1.CurveToTrim.AllowSelfIntersection(True) 193 rules1 = [curveDumbRule1] 194 195 helpPoint1 = NXOpen.Point3d(arc0.CenterPoint.X - 0.1, 196 arc0.CenterPoint.Y, 197 arc0.CenterPoint.Z) 198 trimCurve2Builder1.CurveToTrim.AddToSection( 199 rules1, arc0, NXOpen.NXObject.Null, NXOpen.NXObject.Null, helpPoint1, NXOpen.Section.Mode.Create, False) 200 curves2 = [arc1] 201 curveDumbRule2 = self.workPart.ScRuleFactory.CreateRuleBaseCurveDumb( 202 curves2) 203 trimCurve2Builder1.CurveToTrim.AllowSelfIntersection(True) 204 rules2 = [curveDumbRule2] 205 helpPoint2 = NXOpen.Point3d(arc1.CenterPoint.X + 0.1, 206 arc1.CenterPoint.Y, 207 arc1.CenterPoint.Z) 208 trimCurve2Builder1.CurveToTrim.AddToSection( 209 rules2, arc1, NXOpen.NXObject.Null, NXOpen.NXObject.Null, helpPoint2, NXOpen.Section.Mode.Create, False) 210 211 trimCurveBoundingObjectBuilder1 = trimCurve2Builder1.CreateTrimCurveBoundingObjectBuilder() 212 trimCurve2Builder1.BoundingObjectList.Append( 213 trimCurveBoundingObjectBuilder1) 214 section1 = self. workPart.Sections.CreateSection( 215 0.00095, 0.001, 0.050000000000000003) 216 section1.SetAllowedEntityTypes( 217 NXOpen.Section.AllowTypes.CurvesAndPoints) 218 trimCurveBoundingObjectBuilder1.BoundingObjectList.Add( 219 section1) 220 221 curves3 = [l1.GetEntities()[0]] 222 curveDumbRule3 = self.workPart.ScRuleFactory.CreateRuleBaseCurveDumb( 223 curves3) 224 section1.AllowSelfIntersection(True) 225 rules3 = [curveDumbRule3] 226 227 helpPoint3 = NXOpen.Point3d(0.0, 0.0, 0.0) 228 section1.AddToSection( 229 rules3, l1, NXOpen.NXObject.Null, NXOpen.NXObject.Null, helpPoint3, NXOpen.Section.Mode.Create, False) 230 231 section1.SetAllowedEntityTypes( 232 NXOpen.Section.AllowTypes.OnlyCurves) 233 234 trimCurveBoundingObjectBuilder2 = trimCurve2Builder1.CreateTrimCurveBoundingObjectBuilder() 235 trimCurve2Builder1.BoundingObjectList.Append( 236 trimCurveBoundingObjectBuilder2) 237 238 selectDisplayableObjectList4 = trimCurveBoundingObjectBuilder2.BoundingObjectList 239 selectDisplayableObjectList4.Clear() 240 241 section2 = self.workPart.Sections.CreateSection( 242 0.00095, 0.001, 0.050000000000000003) 243 section2.SetAllowedEntityTypes( 244 NXOpen.Section.AllowTypes.CurvesAndPoints) 245 selectDisplayableObjectList4.Add(section2) 246 247 curves4 = [l2.GetEntities()[0]] 248 curveDumbRule4 = self.workPart.ScRuleFactory.CreateRuleBaseCurveDumb( 249 curves4) 250 section2.AllowSelfIntersection(True) 251 rules4 = [curveDumbRule4] 252 helpPoint6 = NXOpen.Point3d( 253 0.0, 0.0, 0.0) 254 section2.AddToSection( 255 rules4, l2, NXOpen.NXObject.Null, NXOpen.NXObject.Null, helpPoint6, NXOpen.Section.Mode.Create, False) 256 257 trimCurve2Builder1.UpdateTrimRegionsAndDivideLocations() 258 259 trimCurve2Builder1.ResetTrimRegions() 260 261 helpPoint7 = NXOpen.Point3d(-2.0338813720730768, 262 3.5543050080440493, 0.0) 263 helpPoint7 = NXOpen.Point3d( 264 -l * math.sin(fxj * math.pi / 180) + 265 self.double07.Value / 2 * 266 math.cos(60 * math.pi / 180), 267 -l * math.cos(fxj * math.pi / 180) + self.double07.Value / 2 * 268 math.sin(60 * math.pi / 180), 269 0.0) 270 271 trimCurve2Builder1.SelectTrimRegion( 272 helpPoint7) 273 274 helpPoint8 = NXOpen.Point3d(-3.5613174093535811, 275 0.98159691183841247, 0.0) 276 helpPoint8 = NXOpen.Point3d( 277 l * math.sin(fxj * math.pi / 180) + 278 self.double07.Value / 2 * 279 math.cos(240 * math.pi / 180), 280 l * math.cos(fxj * math.pi / 180) + self.double07.Value / 2 * 281 math.sin(240 * math.pi / 180), 282 0.0) 283 trimCurve2Builder1.SelectTrimRegion( 284 helpPoint8) 285 286 nXObject1 = trimCurve2Builder1.Commit() 287 288 trimCurve2Builder1.Destroy() 289 290 except Exception as ex: 291 # ---- Enter your exception handling code here ----- 292 errorCode = 1 293 self.theUI.NXMessageBox.Show( 294 "Block Styler", NXOpen.NXMessageBox.DialogType.Error, str(ex)) 295 296 return errorCode
这个函数在你点击应用的时候执行,也就是功能实现代码,生成所需要的反向曲线及反向器外形轮廓