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#-------------------------------------------------------------------------
6def dialogShown_cb(self):
7try:
8# ---- Enter your callback code here -----
um0.ValueAsString == '圆形返向器':
10                self.double04.Show = True
11                self.double05.Show = True
12                self.double06.Show = False
13                self.double07.Show = False
14else:
15                self.double04.Show = False
16                self.double05.Show = True
17                self.double06.Show = True
18                self.double07.Show = True
19except 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#-------------------------------------------------------------------------
4def update_cb(self, block):
5try:
6if block == um0:
um0.ValueAsString == '圆形返向器':
8                    self.double04.Show = True
9                    self.double05.Show = True
10                    self.double06.Show = False
11                    self.double07.Show = False
12else:
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 -----
18elif block == self.double04:
19# ---- Enter your code here -----
20pass
21elif block == self.double06:
22# ---- Enter your code here -----
23pass
24elif block == self.double07:
25# ---- Enter your code here -----
26pass
27elif block == self.double05:
28# ---- Enter your code here -----
29pass
30elif block == self.double0:
31# ---- Enter your code here -----
32pass
33elif block == self.double01:
34# ---- Enter your code here -----
35pass
36elif block == um01:
37# ---- Enter your code here -----
38pass
39elif block == self.double03:
40# ---- Enter your code here -----
41pass
42elif block == self.double02:
43# ---- Enter your code here -----
44pass
45elif block == self.double08:
46# ---- Enter your code here -----
47pass
48elif block == self.double09:
49# ---- Enter your code here -----
50pass
51elif block == self.double010:
52# ---- Enter your code here -----
53pass
54
55except 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
60return 0
  这个函数在你对对话框⾥各个元素进⾏更改的时候执⾏,例如上⾯代码的⽬的是为了实现在使⽤时切换不同的反向器类型时显⽰不同的反向器参数  1def apply_cb(self):
2        errorCode = 0
3try:
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)
um0.ValueAsString == '圆形返向器':
13                points = f.getPoints_1(
14                    0.1,
15                    self.double05.Value,
16                    self.double04.Value,
17'R'um01.ValueAsString == '右旋'else'L')
18# self.lw.Open()
19# for p in points:
20#    self.lw.WriteLine(str(p))
21else:
22                points = f.getPoints_2(
23                    0.1,
24                    self.double05.Value,
25                    self.double06.Value,
26                    self.double07.Value,
27'R'um01.ValueAsString == '右旋'else'L')
28#==================================================================
29# ⽣成样条曲线
30#==================================================================
31            studioSplineBuilderEx1 = self.workPart.Features.CreateStudioSplineBuilderEx(
32                NXOpen.NXObject.Null)
33            xf, _ = f.getPointF(self.double05.Value)
34for p in points:
35                point = self.workPart.Points.CreatePoint(
36                    NXOpen.Point3d(p[0], p[1], p[2]))
37if 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"
um01.ValueAsString == "右旋":
62                helixBuilder1.TurnDirection = NXOpen.Features.HelixBuilderTurnDirections.RightHand
63else:
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(
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# 圆形返向器轮廓线
um0.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# 扁圆形返向器轮廓线
105else:
um01.ValueAsString == '右旋':
107                    fxj = f.getPhi0(self.double05.Value)
108                    # self.lw.Open()
109                    # self.lw.WriteLine(str(fxj))
writeline使用方法python110                    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 * s(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 * s(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                            s(60 * math.pi / 180),
139                            -l * s(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                            s(60 * math.pi / 180),
147                            l * s(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                            s(240 * math.pi / 180),
167                            -l * s(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                            s(240 * math.pi / 180),
175                            l * s(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(
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                        s(60 * math.pi / 180),
267                        -l * s(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                        s(240 * math.pi / 180),
280                        l * s(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
290except 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
296return errorCode
  这个函数在你点击应⽤的时候执⾏,也就是功能实现代码,⽣成所需要的反向曲线及反向器外形轮廓