Extrudebuilder_ how let user to select the sketch that created programmatically

I've created the sketch programmatically in NX, but stuck in the extrudebuilder where I can't find a way for "Sketch selection" for Extruding.. Here is the recorded journal for Extrudebuilder below. Need help way to call the sketch I created (say "Sketch1" as I am calling it )to replace the recorded sketch


Imports System
Imports NXOpen

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

Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work
Dim displayPart As NXOpen.Part = theSession.Parts.Display

Dim nullNXOpen_Features_Feature As NXOpen.Features.Feature = Nothing
Dim extrudeBuilder1 As NXOpen.Features.ExtrudeBuilder = Nothing
extrudeBuilder1 = workPart.Features.CreateExtrudeBuilder(nullNXOpen_Features_Feature)

Dim section1 As NXOpen.Section = Nothing
section1 = workPart.Sections.CreateSection(0.00095, 0.001, 0.01)

extrudeBuilder1.Section = section1
extrudeBuilder1.AllowSelfIntersectingSection(True)
extrudeBuilder1.DistanceTolerance = 0.001
extrudeBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create

Dim targetBodies1(0) As NXOpen.Body
Dim nullNXOpen_Body As NXOpen.Body = Nothing
targetBodies1(0) = nullNXOpen_Body
extrudeBuilder1.BooleanOperation.SetTargetBodies(targetBodies1)

extrudeBuilder1.Limits.StartExtend.Value.RightHandSide = "0"

extrudeBuilder1.Limits.EndExtend.Value.RightHandSide = "20.5"

extrudeBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Subtract

Dim targetBodies2(0) As NXOpen.Body
Dim body1 As NXOpen.Body = CType(workPart.Bodies.FindObject("SB_MIGRATED_GROUND_PANEL(104)"), NXOpen.Body)

targetBodies2(0) = body1
extrudeBuilder1.BooleanOperation.SetTargetBodies(targetBodies2)

extrudeBuilder1.Draft.FrontDraftAngle.RightHandSide = "2"

extrudeBuilder1.Draft.BackDraftAngle.RightHandSide = "2"

extrudeBuilder1.Offset.StartOffset.RightHandSide = "0"

extrudeBuilder1.Offset.EndOffset.RightHandSide = "0.25"

Dim smartVolumeProfileBuilder1 As NXOpen.GeometricUtilities.SmartVolumeProfileBuilder = Nothing
smartVolumeProfileBuilder1 = extrudeBuilder1.SmartVolumeProfile

smartVolumeProfileBuilder1.OpenProfileSmartVolumeOption = False

smartVolumeProfileBuilder1.CloseProfileRule = NXOpen.GeometricUtilities.SmartVolumeProfileBuilder.CloseProfileRuleType.Fci

theSession.SetUndoMarkName(markId1, "Extrude Dialog")

section1.DistanceTolerance = 0.001

section1.ChainingTolerance = 0.00095

section1.SetAllowedEntityTypes(NXOpen.Section.AllowTypes.OnlyCurves)

Dim features1(0) As NXOpen.Features.Feature
Dim sketchFeature1 As NXOpen.Features.SketchFeature = CType(workPart.Features.FindObject("SKETCH(131)"), NXOpen.Features.SketchFeature)

features1(0) = sketchFeature1
Dim curveFeatureRule1 As NXOpen.CurveFeatureRule = Nothing
curveFeatureRule1 = workPart.ScRuleFactory.CreateRuleCurveFeature(features1)

section1.AllowSelfIntersection(True)

Dim rules1(0) As NXOpen.SelectionIntentRule
rules1(0) = curveFeatureRule1
Dim sketch1 As NXOpen.Sketch = CType(sketchFeature1.FindObject("SKETCH_000"), NXOpen.Sketch)

Dim arc1 As NXOpen.Arc = CType(sketch1.FindObject("Curve Arc73"), NXOpen.Arc)

Dim nullNXOpen_NXObject As NXOpen.NXObject = Nothing

Dim helpPoint1 As NXOpen.Point3d = New NXOpen.Point3d(12.727311938666357, 13.485341484614668, -5.5948246910244111e-13)
section1.AddToSection(rules1, arc1, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen.Section.Mode.Create, False)

Dim direction1 As NXOpen.Direction = Nothing
direction1 = workPart.Directions.CreateDirection(sketch1, NXOpen.Sense.Forward, NXOpen.SmartObject.UpdateOption.WithinModeling)

extrudeBuilder1.Direction = direction1

extrudeBuilder1.Limits.StartExtend.TrimType = NXOpen.GeometricUtilities.Extend.ExtendType.ThroughAll

Dim nullNXOpen_DisplayableObject As NXOpen.DisplayableObject = Nothing

extrudeBuilder1.Limits.StartExtend.Target = nullNXOpen_DisplayableObject

extrudeBuilder1.Limits.EndExtend.Value.RightHandSide = "2"

extrudeBuilder1.ParentFeatureInternal = False

theSession.Preferences.Sketch.CreateInferredConstraints = True

theSession.Preferences.Sketch.ContinuousAutoDimensioning = True

theSession.Preferences.Sketch.DimensionLabel = NXOpen.Preferences.SketchPreferences.DimensionLabelType.Expression

theSession.Preferences.Sketch.TextSizeFixed = True

theSession.Preferences.Sketch.FixedTextSize = 0.12

theSession.Preferences.Sketch.DisplayParenthesesOnReferenceDimensions = True

theSession.Preferences.Sketch.DisplayReferenceGeometry = False

theSession.Preferences.Sketch.ConstraintSymbolSize = 3.0

theSession.Preferences.Sketch.DisplayObjectColor = False

theSession.Preferences.Sketch.DisplayObjectName = True

Dim feature1 As NXOpen.Features.Feature = Nothing
feature1 = extrudeBuilder1.CommitFeature()

extrudeBuilder1.Destroy()

End Sub
End Module

I recorded a journal while extruding a sketch (with the "feature curves" rule); I then edited the journal to use the first sketch it finds in the part navigator instead of the originally chosen sketch. Run the code on a part file that has at least 1 sketch in it.

Since your code creates a sketch before extruding it, replace "sketchFeature1" with a reference to your sketch.

Option Strict Off
Imports System
Imports NXOpen

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

Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work

Dim displayPart As NXOpen.Part = theSession.Parts.Display

' ----------------------------------------------
' Menu: Insert->Design Feature->Extrude...
' ----------------------------------------------
Dim markId1 As NXOpen.Session.UndoMarkId
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")

Dim nullNXOpen_Features_Feature As NXOpen.Features.Feature = Nothing

If Not workPart.Preferences.Modeling.GetHistoryMode Then
Throw (New Exception("Create or edit of a Feature was recorded in History Mode but playback is in History-Free Mode."))
End If

Dim sketchFeature1 As NXOpen.Features.SketchFeature = Nothing
Dim sketch1 As NXOpen.Sketch = Nothing

For Each tempFeat As Features.Feature In theSession.Parts.Work.Features
If TypeOf (tempFeat) Is Features.SketchFeature Then
sketchFeature1 = tempFeat
sketch1 = sketchFeature1.Sketch
Exit For
End If
Next

If IsNothing(sketchFeature1) Then
'no sketch found
Return
End If

Dim extrudeBuilder1 As NXOpen.Features.ExtrudeBuilder
extrudeBuilder1 = workPart.Features.CreateExtrudeBuilder(nullNXOpen_Features_Feature)

Dim section1 As NXOpen.Section
section1 = workPart.Sections.CreateSection(0.00038, 0.0004, 0.5)

extrudeBuilder1.Section = section1

extrudeBuilder1.AllowSelfIntersectingSection(True)

extrudeBuilder1.DistanceTolerance = 0.0004

extrudeBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create

Dim nullNXOpen_Body As NXOpen.Body = Nothing

extrudeBuilder1.BooleanOperation.SetTargetBodies({nullNXOpen_Body})

extrudeBuilder1.Limits.StartExtend.Value.RightHandSide = "0"

extrudeBuilder1.Limits.EndExtend.Value.RightHandSide = "10"

theSession.SetUndoMarkName(markId1, "Extrude Dialog")

section1.DistanceTolerance = 0.0004

section1.ChainingTolerance = 0.00038

section1.SetAllowedEntityTypes(NXOpen.Section.AllowTypes.OnlyCurves)

Dim markId2 As NXOpen.Session.UndoMarkId
markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "section mark")

Dim markId3 As NXOpen.Session.UndoMarkId
markId3 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, Nothing)

Dim curveFeatureRule1 As NXOpen.CurveFeatureRule
curveFeatureRule1 = workPart.ScRuleFactory.CreateRuleCurveFeature({sketchFeature1})

section1.AllowSelfIntersection(True)

Dim rules1(0) As NXOpen.SelectionIntentRule
rules1(0) = curveFeatureRule1

Dim nullNXOpen_NXObject As NXOpen.NXObject = Nothing

Dim helpPoint1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
section1.AddToSection(rules1, nullNXOpen_NXObject, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen.Section.Mode.Create, False)

theSession.DeleteUndoMark(markId3, Nothing)

Dim direction1 As NXOpen.Direction
direction1 = workPart.Directions.CreateDirection(sketch1, NXOpen.Sense.Forward, NXOpen.SmartObject.UpdateOption.WithinModeling)

extrudeBuilder1.Direction = direction1

theSession.DeleteUndoMark(markId2, Nothing)

Dim markId4 As NXOpen.Session.UndoMarkId
markId4 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Extrude")

theSession.DeleteUndoMark(markId4, Nothing)

Dim markId5 As NXOpen.Session.UndoMarkId
markId5 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Extrude")

extrudeBuilder1.ParentFeatureInternal = False

Dim feature1 As NXOpen.Features.Feature
feature1 = extrudeBuilder1.CommitFeature()

theSession.DeleteUndoMark(markId5, Nothing)

theSession.SetUndoMarkName(markId1, "Extrude")

extrudeBuilder1.Destroy()

End Sub
End Module

Changed the code below, but still having an error shown after this code below:

Dim sketchFeature1 As NXOpen.Features.SketchFeature = Nothing
' Dim sketch1 As NXOpen.Sketch = Nothing

For Each tempFeat As Features.Feature In theSession.Parts.Work.Features
If TypeOf (tempFeat) Is Features.SketchFeature Then
sketchFeature1 = tempFeat
iriss_vpt75Sketch = sketchFeature1.Sketch
Exit For
End If
Next

Don't know why, but it is showing an error saying below:


NXOpen.NXException: Misssing section string.

at

section1.AddToSection(rules2, nullNXOpen_NXObject, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen.Section.Mode.Create, False)

Regards,
MFJ

Did the journal that I posted earlier work when run on your file, or did it also error out?

It errored out with the same error...

Regards,
MFJ

What version of NX are you using?

Did my journal error out for you at the same point in the code as your version?

Using NX12

Yes it error out at the same point...

Regards,
MFJ

Can you send me your code along with a simple example part file?
info@nxjournaling.com

I've sent you the vb file and the dowwnloaded part (since I work with teamcenter). Thanks!

Regards,
MFJ

The first sketch in the feature tree is empty (or at least has no "extrudable" elements); this is a condition that my code does not check for. If you delete this sketch, my code works as expected on the sketch with the circles in it.

Also, you are using NX 1859 - not NX 12; but I don't think the version is affecting the particular issue that you are having.

After removing that empty sketch, it worked. Having another issue though, as I have added following code to subtract the extrude using the sketch. I added user body selection function, but still saying "Tool body completely outside target body"

extrudeBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Subtract
Dim targetBodies2(0) As NXOpen.Body
Dim body1 As NXOpen.Body = Nothing 'CType(workPart.Bodies.FindObject("SB_MIGRATED_GROUND_PANEL(104)"), NXOpen.Body)
If SelectBody("Select the body to create hole", body1) = Selection.Response.Cancel Then
Return

End If
targetBodies2(0) = body1
extrudeBuilder1.BooleanOperation.SetTargetBodies(targetBodies2)
extrudeBuilder1.Draft.FrontDraftAngle.RightHandSide = "2"
extrudeBuilder1.Draft.BackDraftAngle.RightHandSide = "2"
extrudeBuilder1.Offset.StartOffset.RightHandSide = "0"
extrudeBuilder1.Offset.EndOffset.RightHandSide = "0.25"

Regards,
MFJ

I figured that subtract part out... I just had to add following line:

extrudeBuilder1.Limits.EndExtend.Value.RightHandSide = "-2"

Thanks a lot for the help!

Regards,
MFJ

In interactive NX, you can change the limits to "through all" to ensure that it intersects the target, no matter how it changes in the future. Using this option will make your model more robust. I'm not sure of the NXOpen equivalent, but recording a journal should show you the required syntax.

Also, I'd suggest altering the code to pass in the reference to your sketch feature directly. My code iterates through the features and works with the first sketch just to illustrate that the "selection stickiness" has been removed from the recorded journal code.

Can you please suggest how should I do that, as the code actually failed later while I was trying to another part that had other sketches already.

Also you've probably seen from my code that I am using sketchinplace builder to create a Nxopen.Sketch instead of creating a "Sketch feature"...

Is there any way to convert the Nxopen. sketch to a sketch feature instead of trying to do it the other way around?

Regards,
MFJ

The call to the sketch in place builder's .Commit method will probably return a sketch feature (I've not tested, but I'm fairly certain it will). If it returns a sketch object, you can get the sketch feature by using the sketch's .Feature property.

Thanks... I'll try to do as you said..

Regards,
MFJ

It worked as it removed the selection stickiness by using sketchname.Feature as sketchfeature ... Thanks for the idea!

The sketchinplacebuilder actully returns the NXopen.sketch directly, so couldn't do that.

Regards,
MFJ