×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Hole to slot

Hole to slot

Hole to slot

(OP)
We have a running joke at work rev A is a hole, rev B is a bigger hole and rev C is a slot. Which inspired me to write the below code. I would like a bit of help to make it more robust in what face it selects (ie hole/cylindrical face only) and also allow more options for slot direct other than just face normal. Also if anyone has other ways to improve this code please hack the code to suit.

This command works on c'sk and c'bore holes as well and with a few tweaks to the features in the part navigator you can switch between hole, c'sk and c'bore if the hole is created from a hole feature.

I plan to also submit an ER to see if Siemens can write something like this into the hole command. In the mean time this works ok, I find it quicker and easier to use than the OOTB slot command, especially if holes already exist.

CODE -->

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Utilities

Module NXJournalHoleToSlot
    Sub Main(ByVal args() As String)

        Dim theSession As Session = Session.GetSession()
        Dim workPart As Part = theSession.Parts.Work
        Dim ufs As UFSession = UFSession.GetUFSession()
        Dim displayPart As Part = theSession.Parts.Display

        ' ----------------------------------------------
        '   Menu: Insert->Datum/Point->Datum Plane...
        ' ----------------------------------------------
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

        Dim nullFeatures_Feature As Features.Feature = Nothing

        Dim datumPlaneBuilder1 As Features.DatumPlaneBuilder
        datumPlaneBuilder1 = workPart.Features.CreateDatumPlaneBuilder(nullFeatures_Feature)

        Dim plane1 As Plane
        plane1 = datumPlaneBuilder1.GetPlane()

        plane1.SetUpdateOption(SmartObject.UpdateOption.WithinModeling)

        Dim face1 As Face = select_a_face("Select Hole Face")

        Dim line1 As Line
        line1 = workPart.Lines.CreateFaceAxis(face1, SmartObject.UpdateOption.WithinModeling)

        plane1.SetMethod(PlaneTypes.MethodType.Angle)

        Dim geom1(1) As NXObject

        Dim face2 As Face = select_a_face("Select Face Normal To Slot Direction")

        geom1(0) = face2
        geom1(1) = line1
        plane1.SetGeometry(geom1)

        plane1.SetExpression("0")

        plane1.SetAlternate(PlaneTypes.AlternateType.One)

        plane1.RemoveOffsetData()

        plane1.Evaluate()

        datumPlaneBuilder1.ResizeDuringUpdate = True

        Dim datumFeature As Features.Feature
        datumFeature = datumPlaneBuilder1.CommitFeature()

        datumPlaneBuilder1.Destroy()

        Dim adjBefore() As Tag = Nothing
        ufs.Modl.AskAdjacFaces(face1.Tag, adjBefore)

        ' ----------------------------------------------
        '   Menu: Insert->Trim->Divide Face...
        ' ----------------------------------------------

        nullFeatures_Feature = Nothing

        Dim dividefaceBuilder1 As Features.DividefaceBuilder
        dividefaceBuilder1 = workPart.Features.CreateDividefaceBuilder(nullFeatures_Feature)

        dividefaceBuilder1.SelectDividingObject.OffsetDistance.RightHandSide = "5"

        dividefaceBuilder1.BlankOption = True

        Dim projectionOptions1 As GeometricUtilities.ProjectionOptions
        projectionOptions1 = dividefaceBuilder1.ProjectionOption

        Dim section1 As Section
        section1 = dividefaceBuilder1.SelectDividingObject.CurvesToOffset

        section1.DistanceTolerance = 0.0254

        section1.ChainingTolerance = 0.02413

        Dim scCollector1 As ScCollector
        scCollector1 = workPart.ScCollectors.CreateCollector()

        Dim boundaryFaces1(-1) As Face
        Dim faceTangentRule1 As FaceTangentRule
        faceTangentRule1 = workPart.ScRuleFactory.CreateRuleFaceTangent(face1, boundaryFaces1, 0.1)

        Dim rules1(0) As SelectionIntentRule
        rules1(0) = faceTangentRule1
        scCollector1.ReplaceRules(rules1, False)

        dividefaceBuilder1.FacesToDivide = scCollector1

        Dim section2 As Section
        section2 = workPart.Sections.CreateSection(0.02413, 0.0254, 0.1)

        section2.SetAllowedEntityTypes(Section.AllowTypes.OnlyCurves)

        Dim scCollector2 As ScCollector
        scCollector2 = workPart.ScCollectors.CreateCollector()

        Dim faces1(0) As DatumPlane
        Dim datumFeat As Features.DatumFeature = CType(datumFeature, Features.DatumFeature)

        Dim linkedTag As NXOpen.Tag = Nothing
        Dim numObjs As Integer = -1
        Dim tags() As NXOpen.Tag
        ufs.Modl.AskFeatObject(datumFeature.Tag, numObjs, tags)
            linkedTag = tags(0)
        Dim datumPlane1 As DatumPlane = CType(NXObjectManager.Get(linkedTag), DatumPlane)

        faces1(0) = datumPlane1
        Dim faceDumbRule1 As FaceDumbRule
        faceDumbRule1 = workPart.ScRuleFactory.CreateRuleFaceDatum(faces1)

        Dim rules2(0) As SelectionIntentRule
        rules2(0) = faceDumbRule1
        scCollector2.ReplaceRules(rules2, False)

        Dim added1 As Boolean
        added1 = dividefaceBuilder1.SelectDividingObject.DividingObjectsList.Add(scCollector2)

        projectionOptions1.ProjectDirectionMethod = GeometricUtilities.ProjectionOptions.DirectionType.FaceNormal

        Dim divideFaceFeature As Features.Feature
        divideFaceFeature = dividefaceBuilder1.CommitFeature()

        dividefaceBuilder1.Destroy()

        section2.Destroy()

        Dim faceFound As Boolean = False
        Dim newFace As Face
        Dim adjAfter() As Tag = Nothing
        ufs.Modl.AskAdjacFaces(face1.Tag, adjAfter)
        For Each tag As NXOpen.Tag In adjAfter
            For Each tag2 As NXOpen.Tag In adjBefore
                If NXObjectManager.Get(tag).ToString = NXObjectManager.Get(tag2).ToString Then
                    faceFound = True
                    Exit For
                End If
            Next
            If faceFound = False Then
                newFace = CType(NXObjectManager.Get(tag), Face)
                Exit For
            End If
            faceFound = False
        Next

        ' ----------------------------------------------
        '   Menu: Insert->Synchronous Modeling->Move Face...
        ' ----------------------------------------------
        Dim nullFeatures_AdmMoveFace As Features.AdmMoveFace = Nothing

        Dim admMoveFaceBuilder1 As Features.AdmMoveFaceBuilder
        admMoveFaceBuilder1 = workPart.Features.CreateAdmMoveFaceBuilder(nullFeatures_AdmMoveFace)

        admMoveFaceBuilder1.Motion.Option = GeometricUtilities.ModlMotion.Options.Distance

        admMoveFaceBuilder1.FaceChangeOverflowBehavior.FaceChangeOption = GeometricUtilities.FaceChangeOverflowBehavior.Option.ExtendCapFace

        Dim faces2(0) As Face

        faces2(0) = face1
        Dim faceDumbRule2 As FaceDumbRule
        faceDumbRule2 = workPart.ScRuleFactory.CreateRuleFaceDumb(faces2)

        Dim rules3(0) As SelectionIntentRule
        rules3(0) = faceDumbRule2
        admMoveFaceBuilder1.FaceToMove.FaceCollector.ReplaceRules(rules3, False)

        Dim direction1 As Direction
        direction1 = workPart.Directions.CreateDirection(datumPlane1, Sense.Reverse, SmartObject.UpdateOption.WithinModeling)

        admMoveFaceBuilder1.Motion.DistanceVector = direction1

        Dim slotLen As Double

        slotLen = InputBox("Enter slot length", "Slot Length")

        admMoveFaceBuilder1.Motion.DistanceValue.RightHandSide = CStr(slotLen / 2)

        Dim nXObject1 As NXObject
        nXObject1 = admMoveFaceBuilder1.Commit()
        Dim moveFeat1 As Features.Feature = admMoveFaceBuilder1.GetFeature
        admMoveFaceBuilder1.Destroy()

        Dim exprs() As Expression = moveFeat1.GetExpressions()
        Dim moveExp As String = ""
        For Each exp As Expression In exprs
            moveExp = exp.Name
        Next

        ' ----------------------------------------------
        '   Menu: Insert->Synchronous Modeling->Move Face...
        ' ----------------------------------------------
        Dim nullFeatures_AdmMoveFace2 As Features.AdmMoveFace = Nothing

        Dim admMoveFaceBuilder2 As Features.AdmMoveFaceBuilder
        admMoveFaceBuilder2 = workPart.Features.CreateAdmMoveFaceBuilder(nullFeatures_AdmMoveFace2)

        admMoveFaceBuilder2.Motion.Option = GeometricUtilities.ModlMotion.Options.Distance

        admMoveFaceBuilder2.FaceChangeOverflowBehavior.FaceChangeOption = GeometricUtilities.FaceChangeOverflowBehavior.Option.Automatic

        Dim faces3(0) As Face

        faces3(0) = newFace
        Dim faceDumbRule3 As FaceDumbRule
        faceDumbRule3 = workPart.ScRuleFactory.CreateRuleFaceDumb(faces3)

        Dim rules4(0) As SelectionIntentRule
        rules4(0) = faceDumbRule3
        admMoveFaceBuilder2.FaceToMove.FaceCollector.ReplaceRules(rules4, False)

        Dim direction2 As Direction
        direction2 = workPart.Directions.CreateDirection(datumPlane1, Sense.Forward, SmartObject.UpdateOption.WithinModeling)

        admMoveFaceBuilder2.Motion.DistanceVector = direction2

        admMoveFaceBuilder2.Motion.DistanceValue.RightHandSide = moveExp

        Dim nXObject2 As NXObject
        nXObject2 = admMoveFaceBuilder2.Commit()
        Dim moveFeat2 As Features.Feature = admMoveFaceBuilder2.GetFeature
        admMoveFaceBuilder2.Destroy()

        ' ----------------------------------------------
        '   Dialog Begin Feature Group
        ' ----------------------------------------------

        Dim toBeGrouped(3) As Features.Feature ' = selectFeatures("Create Feature Group")
        toBeGrouped(0) = datumFeature
        toBeGrouped(1) = divideFaceFeature
        toBeGrouped(2) = moveFeat1
        toBeGrouped(3) = moveFeat2

        Dim featTags(toBeGrouped.Length - 1) As Tag
        Dim latestTimestamp As Integer = 0
        Dim latestFeature As Features.Feature = Nothing
        Dim oldLatest As Tag = Nothing
        ufs.Modl.AskCurrentFeature(theSession.Parts.Work.Tag, oldLatest)

        For ii As Integer = 0 To toBeGrouped.Length - 1
            featTags(ii) = toBeGrouped(ii).Tag
            If toBeGrouped(ii).Timestamp > latestTimestamp Then
                latestTimestamp = toBeGrouped(ii).Timestamp
                latestFeature = toBeGrouped(ii)
            End If
        Next
        latestFeature.MakeCurrentFeature()
        Dim fGroup As Tag = Tag.Null
        ufs.Modl.CreateSetOfFeature("Hole_To_Slot", featTags, toBeGrouped.Length, 1, fGroup)

        latestFeature = NXObjectManager.Get(fGroup)
        latestFeature.MakeCurrentFeature()

    End Sub
    Function select_a_face(ByRef prompt As String) As Face

        Dim mask() As Selection.MaskTriple = {New Selection.MaskTriple( _
            UFConstants.UF_solid_type, 0, UFConstants.UF_UI_SEL_FEATURE_ANY_FACE)}
        Dim cursor As Point3d = Nothing
        Dim sel_obj As TaggedObject = Nothing

        UI.GetUI.SelectionManager.SelectTaggedObject(prompt, prompt, _
            Selection.SelectionScope.AnyInAssembly, _
            Selection.SelectionAction.ClearAndEnableSpecific, False, _
            False, mask, sel_obj, cursor)

        Return CType(sel_obj, Face)

    End Function

End Module 

NX 9.0.3.4 mp12, TC 10.1

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources