Forums:
Hello
I've put together a code for thread engraving. The problem is that it is not updating when the thread size changes, even thou reference text is checked on. How to make it parametric?
Here's the code
Imports System Imports NXOpen Imports NXOpen.UFImports NXOpen.UtilitiesImports MiniSnap Imports NXOpen.FeaturesImports System.Text.RegularExpressionsImports System.IO Module Text_on_face Public theSession As NXOpen.Session= NXOpen.Session.GetSession()Public theUfSession As UFSession = UFSession.GetUFSession()Public workPart As NXOpen.Part= theSession.Parts.WorkPublic ui As UI = UI.GetUI()Public ufs As UFSession = UFSession.GetUFSession()Public prompt AsString="Select Position on Face"Public temptag As Tag = Tag.NullPublic cp(2)AsDoublePublic face1 As Face =NothingPublic facetag As Tag = Tag.NullPublic pointC As Point =NothingPublic displayPart As NXOpen.Part= theSession.Parts.DisplayPublic ok_click AsBooleanPublic text2 AsStringPublic TextLayer AsInteger=45Public text AsStringPublic lw As ListingWindow = theSession.ListingWindowDim myGeo As Features.FeatureDim cancel AsBoolean=False Sub Main() Dim lw As ListingWindow = theSession.ListingWindow lw.Open() Start1: myGeo = SelectSymbolicThread("Select symbolic thread")If myGeo IsNothingThen cancel =TrueExitSubEndIf If myGeo.FeatureType.Equals("SYMBOLIC_THREAD")Then' lw.WriteLine(myGeo.GetFeatureName())Dim data As UFModl.SymbThreadData=New UFModl.SymbThreadData() theUfSession.Modl.AskSymbThreadParms(myGeo.Tag, data) Dim str AsString= data.major_diaDim leftPart AsString= str.Split("=")(0)Dim expression1 As NXOpen.Expression=NothingDim theExp As NXOpen.Expression=NothingDim thr_exp As NXOpen.Expression=Nothing Dim faceid AsString= data.cyl_face.ToString()Dim expression_a AsString="thr_txt"&"_"& faceid Try expression1 = workPart.Expressions.CreateExpression("String", expression_a &"=""M""+stringValue( "& leftPart &" )")Catch ex As NXException If ex.ErrorCode=1050017Then'expression already exists theExp = workPart.Expressions.FindObject(expression_a) workPart.Expressions.Edit(theExp, """M""+stringValue( "& leftPart &")")ElseEndIfEndTry thr_exp = workPart.Expressions.FindObject(expression_a) text =""& thr_exp.StringValue&"" ' lw.WriteLine(text) EndIf facetag = select_point_on_face(prompt, cp) If facetag = Tag.NullThenGoTo End1 face1 = NXObjectManager.Get(facetag)Dim pnt3d As Point3d =New Point3d(cp(0), cp(1), cp(2)) ' ----------------------------------------------Dim ufmodl As UFModl = theUfSession.ModlDim faceType AsIntegerDim facePt AsDouble()=NewDouble(2){}Dim faceDir AsDouble()=NewDouble(2){}Dim bbox AsDouble()=NewDouble(5){}Dim faceRadius AsDoubleDim faceRadData AsDoubleDim normDirection AsIntegerDim bodyObjFaceID As Tag ufmodl.AskFaceData(facetag, faceType, facePt, faceDir, bbox, faceRadius, faceRadData, normDirection)Dim dir As Vector = faceDir Dim uvminmax AsDouble()=NewDouble(3){} ufmodl.AskFaceUvMinmax(facetag, uvminmax) ufmodl.AskFaceBody(facetag, bodyObjFaceID)Dim umin AsDouble= uvminmax(0)Dim vmin AsDouble= uvminmax(2)Dim surfaceValue As NXOpen.UF.ModlSrfValue=New NXOpen.UF.ModlSrfValue()Dim mode AsInteger= NXOpen.UF.UFConstants.UF_MODL_EVAL_UNIT_NORMAL ufmodl.EvaluateFace(facetag, mode, NewDouble(){umin, vmin}, surfaceValue)Dim u As Vector = surfaceValue.srf_duDim v As Vector = Vector.Cross(dir, u)Dim oriview As MiniSnap.Orientation=New MiniSnap.Orientation(workPart.ModelingViews.WorkView.Matrix)Dim a0 AsDouble= u * oriview.AxisX+ v * oriview.AxisYDim a1 AsDouble= v * oriview.AxisX- u * oriview.AxisYDim a2 AsDouble=-u * oriview.AxisX- v * oriview.AxisYDim faceA As NXOpen.Face= face1 Dim text2DScale AsDouble=130.0Dim text3DScale AsDouble=80Dim nullNXOpen_Features_Text As NXOpen.Features.Text=Nothing ' ----------------------------------------------Dim markId1 As NXOpen.Session.UndoMarkId=Nothing markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start") ' ------------- Text Builder - START -----------------------------------------------Dim unit1 As NXOpen.Unit=Nothing Dim textBuilder2 As NXOpen.Features.TextBuilder=Nothing textBuilder2 = workPart.Features.CreateTextBuilder(nullNXOpen_Features_Text) unit1 = textBuilder2.PrintMarkThickness.Units textBuilder2.CanReferenceText=True textBuilder2.PlanarFrame.AnchorLocation= NXOpen.GeometricUtilities.RectangularFrameBuilder.AnchorLocationType.MiddleCenter textBuilder2.PlanarFrame.WScale= text3DScale textBuilder2.PlanarFrame.Shear.RightHandSide="0" textBuilder2.PrintMarkThickness.RightHandSide="1" textBuilder2.FrameOnPath.AnchorPosition.Expression.RightHandSide="50" textBuilder2.PlanarFrame.Length.RightHandSide="10" textBuilder2.PlanarFrame.Height.RightHandSide="7" textBuilder2.TextString=""& text &"" textBuilder2.SelectFont("Arial", NXOpen.Features.TextBuilder.ScriptOptions.CentralEuropean) theSession.SetUndoMarkName(markId1, "Text Dialog")Dim xform2 As NXOpen.Xform=Nothing xform2 = workPart.Xforms.CreateXform(faceA, NXOpen.SmartObject.UpdateOption.WithinModeling)Dim cartesianCoordinateSystem2 As NXOpen.CartesianCoordinateSystem=Nothing cartesianCoordinateSystem2 = workPart.CoordinateSystems.CreateCoordinateSystem(pnt3d, u, v) textBuilder2.PlanarFrame.CoordinateSystem= cartesianCoordinateSystem2 ufs.Obj.DeleteObject(cartesianCoordinateSystem2.Tag) textBuilder2.PlanarFrame.UpdateOnCoordinateSystem() cartesianCoordinateSystem2.SetVisibility(SmartObject.VisibilityOption.Invisible) cartesianCoordinateSystem2.Evaluate() theSession.UpdateManager.ClearErrorList()Dim nXObject2 As NXOpen.NXObject=Nothing nXObject2 = textBuilder2.Commit() textBuilder2.Destroy() Dim allFeats()As Feature = displayPart.Features.ToArray()Dim topFeat AsInteger= allFeats.GetUpperBound(0)Dim lastFeature As Feature = allFeats(topFeat)Dim myCurve As Curve Dim featTag As Tag = Tag.NullDim myFeature As Features.Feature myFeature = Utilities.NXObjectManager.Get(lastFeature.Tag)' lw.WriteLine("used by: " & myFeature.GetFeatureName) Dim FeatStr AsString= myFeature.ToString.Substring(0, 4) If FeatStr ="Text"ThenDim numObjs AsIntegerDim objs()As NXOpen.Tag=Nothing ufs.Modl.AskFeatObject(myFeature.Tag, numObjs, objs)ForEach thisObj As NXOpen.TagIn objs ufs.Obj.SetLayer(thisObj, TextLayer)Next EndIf Dim stateArray1(0)As NXOpen.Layer.StateInfo stateArray1(0)=New NXOpen.Layer.StateInfo(TextLayer, NXOpen.Layer.State.Hidden) workPart.Layers.ChangeStates(stateArray1, False) Dim section1 As Section section1 = theSession.Parts.Work.Sections.CreateSection(0.00038, 0.0004, 0.5) section1.SetAllowedEntityTypes(Section.AllowTypes.OnlyCurves)Dim curveFeatureRule1 As CurveFeatureRule curveFeatureRule1 = theSession.Parts.Work.ScRuleFactory.CreateRuleCurveFeature({myFeature}) section1.AllowSelfIntersection(True)Dim rules1(0)As SelectionIntentRule rules1(0)= curveFeatureRule1 Dim nullNXObject As NXObject =Nothing section1.AddToSection(rules1, myCurve, nullNXObject, nullNXObject, Nothing, Section.Mode.Create, False) Dim nullFeatures_Feature As Features.Feature=NothingDim extrudeBuilder1 As Features.ExtrudeBuilder extrudeBuilder1 = theSession.Parts.Work.Features.CreateExtrudeBuilder(nullFeatures_Feature) extrudeBuilder1.Section= section1 extrudeBuilder1.Limits.SymmetricOption=True extrudeBuilder1.Limits.StartExtend.Value.RightHandSide=".1" extrudeBuilder1.Limits.EndExtend.Value.RightHandSide=".1" extrudeBuilder1.Limits.StartExtend.TrimType= NXOpen.GeometricUtilities.Extend.ExtendType.Symmetric extrudeBuilder1.Limits.EndExtend.TrimType= NXOpen.GeometricUtilities.Extend.ExtendType.Symmetric extrudeBuilder1.AllowSelfIntersectingSection(True) extrudeBuilder1.DistanceTolerance=0.001 extrudeBuilder1.BooleanOperation.Type= GeometricUtilities.BooleanOperation.BooleanType.Create extrudeBuilder1.BooleanOperation.Type= NXOpen.GeometricUtilities.BooleanOperation.BooleanType.SubtractDim The_Body As NXOpen.Body= NXObjectManager.Get(bodyObjFaceID)Dim targetBodies2(0)As Body targetBodies2(0)= The_Body extrudeBuilder1.BooleanOperation.SetTargetBodies(targetBodies2)Dim origin1 As Point3d =New Point3d(0.0, 0.0, 0.0)Dim vector1 As Vector3d =New Vector3d(0.0, 0.0, 1.0)Dim direction1 As Direction direction1 = theSession.Parts.Work.Directions.CreateDirection(pnt3d, dir, SmartObject.UpdateOption.WithinModeling) extrudeBuilder1.Direction= direction1 extrudeBuilder1.Draft.DraftOption= GeometricUtilities.SimpleDraft.SimpleDraftType.NoDraft extrudeBuilder1.ParentFeatureInternal=FalseDim feature1 As Features.Feature feature1 = extrudeBuilder1.CommitFeature() extrudeBuilder1.Destroy() ' ------------------------ Text Builder - END ----------------------------------------------- GoTo Start1 End1: EndSub PublicSub map_view2abs(ByRef c()AsDouble)Dim vname AsString=""Dim abs_mx()AsDouble={0, 0, 0, 1, 0, 0, 0, 1, 0}Dim vw()AsDouble={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}Dim mx()AsDouble={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}Dim irc AsInteger=0 ufs.Ui.AskLastPickedView(vname)Dim wp As Part = theSession.Parts.WorkDim lastViewPicked As NXOpen.View=CType(wp.ModelingViews.FindObject(vname), ModelingView)Dim vmx As Matrix3x3 = lastViewPicked.Matrix() vw(3)= vmx.Xx vw(4)= vmx.Xy vw(5)= vmx.Xz vw(6)= vmx.Yx vw(7)= vmx.Yy vw(8)= vmx.Yz vw(9)= vmx.Zx vw(10)= vmx.Zy vw(11)= vmx.Zz ufs.Trns.CreateCsysMappingMatrix(vw, abs_mx, mx, irc) ufs.Trns.MapPosition(c, mx)EndSub PublicSub map_abs2view(ByRef c()AsDouble)Dim vname AsString=""Dim abs_mx()AsDouble={0, 0, 0, 1, 0, 0, 0, 1, 0}Dim vw()AsDouble={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}Dim mx()AsDouble={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}Dim irc AsInteger=0 ufs.Ui.AskLastPickedView(vname)Dim wp As Part = theSession.Parts.WorkDim lastViewPicked As NXOpen.View=CType(wp.ModelingViews.FindObject(vname), ModelingView)Dim vmx As Matrix3x3 = lastViewPicked.Matrix() vw(3)= vmx.Xx vw(4)= vmx.Xy vw(5)= vmx.Xz vw(6)= vmx.Yx vw(7)= vmx.Yy vw(8)= vmx.Yz vw(9)= vmx.Zx vw(10)= vmx.Zy vw(11)= vmx.Zz ufs.Trns.CreateCsysMappingMatrix(abs_mx, vw, mx, irc) ufs.Trns.MapPosition(c, mx) EndSub PublicSub ask_pos_on_obj(ByVal obj As NXOpen.Tag, ByValloc()AsDouble)Dim aLine As NXOpen.Tag= NXOpen.Tag.NullDim cp()AsDouble={0, 0, 0}Dim dist AsDouble=0Dim lp As UFCurve.LineDim sp(2)AsDoubleDim ep(2)AsDouble lp.start_point= sp lp.end_point= ep map_abs2view(loc) lp.start_point(0)=loc(0) lp.start_point(1)=loc(1) lp.start_point(2)=loc(2)+10000 lp.end_point(0)=loc(0) lp.end_point(1)=loc(1) lp.end_point(2)=loc(2)-10000 map_view2abs(lp.start_point) map_view2abs(lp.end_point) ufs.Curve.CreateLine(lp, aLine) ufs.Modl.AskMinimumDist(obj, aLine, 0, cp, 0, cp, dist, loc, cp) ufs.Obj.DeleteObject(aLine)EndSub PublicFunction mask_for_face(ByVal select_ As IntPtr, ByVal userdata As IntPtr)AsInteger Dim num_triples AsInteger=1Dim mask_triples(0)As UFUi.Mask mask_triples(0).object_type= UFConstants.UF_solid_type mask_triples(0).object_subtype=0 mask_triples(0).solid_type= UFConstants.UF_UI_SEL_FEATURE_ANY_FACE ufs.Ui.SetSelMask(select_, UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, num_triples, mask_triples)Return UFConstants.UF_UI_SEL_SUCCESSEndFunction PublicFunction select_point_on_face(ByVal prompt AsString, ByRef cp()AsDouble)As NXOpen.TagDim resp AsInteger=0Dim thing As NXOpen.Tag= NXOpen.Tag.NullDim face1 As Face =NothingDim theView As NXOpen.Tag= NXOpen.Tag.NullDim mask_face As UFUi.SelInitFnT=AddressOf mask_for_face Dim facetag As Tag = Tag.Null ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) ufs.Ui.SelectWithSingleDialog("Select a face", prompt, UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY, mask_face, Nothing, resp, facetag, cp, theView) ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)If resp = UFConstants.UF_UI_OBJECT_SELECTEDOr resp = UFConstants.UF_UI_OBJECT_SELECTED_BY_NAMEThen ask_pos_on_obj(facetag, cp) ufs.Disp.SetHighlight(facetag, 0)Return facetag EndIfReturn Tag.NullEndFunction PublicFunction PointFeatureOnFace(ByValwhereAs Point3d)As Point Dim markId1 As Session.UndoMarkId= theSession.SetUndoMark(Session.MarkVisibility.Visible, "Create Point")Dim point1 As Point point1 = workPart.Points.CreatePoint(where)Dim nullFeatures_Feature As Features.Feature=NothingDim pointFeatureBuilder1 As Features.PointFeatureBuilder pointFeatureBuilder1 = workPart.BaseFeatures.CreatePointFeatureBuilder(nullFeatures_Feature) pointFeatureBuilder1.Point= point1 Dim nXObject1 As NXObject = pointFeatureBuilder1.Commit() pointFeatureBuilder1.Destroy()Return point1 EndFunction Function SelectSymbolicThread(ByVal prompt AsString)As Features.Feature Dim response AsInteger=0Dim objs()As Tag =NothingDim user_data As System.IntPtrDim selTag As Tag =NothingDim cursor(2)AsDoubleDim view As Tag =Nothing theUfSession.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) Dim cursorView AsInteger theUfSession.Ui.AskCursorView(cursorView) theUfSession.Ui.SetCursorView(0) Try theUfSession.Ui.SelectWithSingleDialog("Select symbolic thread arc: ", prompt, UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY, AddressOf init_proc_body, user_data, response, selTag, cursor, view)Finally theUfSession.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) theUfSession.Ui.SetCursorView(cursorView)EndTry IfNot selTag.Equals(Tag.Null)Then 'the SelectWithSingleDialog method returns a tag,'return the object from the given tagDim myCurve As Curve = Utilities.NXObjectManager.Get(selTag)Dim featTag As Tag = Tag.NullDim myFeature As Features.Feature=Nothing theUfSession.Modl.AskObjectFeat(myCurve.Tag, featTag) myFeature = Utilities.NXObjectManager.Get(featTag) theUfSession.Disp.SetHighlight(selTag, 0)Return myFeature ElseReturnNothingEndIf EndFunction Function init_proc_body(ByVal select_ As IntPtr, ByVal userdata As IntPtr)AsInteger'this function must have the same signature as UFUi.SelInitFnT Delegate 'setup mask to filter for arcs, lines, and silhouette curvesDim mask_triples As UFUi.Mask()=New UFUi.Mask(1){} mask_triples(0).object_type= UFConstants.UF_circle_type mask_triples(0).object_subtype=0 mask_triples(0).solid_type=0 mask_triples(1).object_type= UFConstants.UF_line_type mask_triples(1).object_subtype=0 mask_triples(1).solid_type=0 theUfSession.Ui.SetSelMask(select_, UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, mask_triples.Length, mask_triples) theUfSession.Ui.SetSelProcs(select_, AddressOf HoleFeatureFilter, Nothing, userdata) Return UFConstants.UF_UI_SEL_SUCCESS EndFunction Function HoleFeatureFilter(ByVal _object As Tag, ByVal type AsInteger(), ByVal user_data As IntPtr, ByVal select_ As IntPtr)AsInteger'type, user_data, and select_ are unused (in this implementation), but this function must have'the same signature as UFUi.SelFilterFnT Delegate Dim myCurve As Curve = Utilities.NXObjectManager.Get(_object)Dim featTag As Tag = Tag.NullDim myFeature As Features.Feature=Nothing theUfSession.Modl.AskObjectFeat(myCurve.Tag, featTag) myFeature = Utilities.NXObjectManager.Get(featTag)If myFeature.FeatureType="SYMBOLIC_THREAD"ThenReturn UFConstants.UF_UI_SEL_ACCEPTElseReturn UFConstants.UF_UI_SEL_REJECTEndIf EndFunction PublicFunction GetUnloadOption(ByVal dummy AsString)AsInteger'----Other unload options-------'Unloads the image immediately after execution within NX GetUnloadOption = NXOpen.Session.LibraryUnloadOption.ImmediatelyEndFunction EndModule