Programing Sketch Constraint Examples
Programing Sketch Constraint Examples
(OP)
Does anyone know where I could find examples of how to program sketch constraints, I'm using vb. The recorded journal has very specific point3d, which in a program these are going to be in different places in space and I'm not sure how to generalize these or even if they are necessary, if the point is not used is there a way to dimension and not tell it where to place it.
An example of a simple dimensioned constrained sketch program would be crazy useful. There isn't any I can find at the GTAC Solutions center and none have been posted here or on the Siemens forums. I can post the recorded journal if anyone is interested.
Thanks in advance.
An example of a simple dimensioned constrained sketch program would be crazy useful. There isn't any I can find at the GTAC Solutions center and none have been posted here or on the Siemens forums. I can post the recorded journal if anyone is interested.
Thanks in advance.





RE: Programing Sketch Constraint Examples
CODE
Dim startPoint1 As Point = New Point3d(3.26829028699467, -35.0000000000009, 0)
Dim endPoint1 As Point = New Point3d(33.0495132264591, 38.7111164380587, 0)
Dim line1 As Line
line1 = workPart.Curves.CreateLine(startPoint1, endPoint1)
But it's so specific in the actual point location. I need the sketch to be on the plane, the specific plane that was selected for the sketch to work on, thus a two dimensional surface where there would be no reference to the Z. The only line commands I find require points or point3d.
Any example sketch or help in any other way, would be greatly appreciated.
RE: Programing Sketch Constraint Examples
www.nxjournaling.com
RE: Programing Sketch Constraint Examples
I have only one module, I have the class copied directly over, with no changes, inside the module.
I have a Place sketch class, where I'm trying to call the transform and reset the class each time I create a new sketch. Then I am calling separate sub routines for each line and spline, where I am wanting to use the translation.
CODE
Dim transform As transform
Dim nullSketch As Sketch = Nothing
Dim unit1 As Unit = CType(workPart.UnitCollection.FindObject("MilliMeter"), Unit)
Dim sketchInPlaceBuilder1 As SketchInPlaceBuilder
sketchInPlaceBuilder1 = workPart.Sketches.CreateNewSketchInPlaceBuilder(nullSketch)
Dim section1 As Section
section1 = workPart.Sections.CreateSection(0.02413, 0.0254, 0.5)
Dim section2 As Section
section2 = workPart.Sections.CreateSection(0.02413, 0.0254, 0.5)
Dim sketchAlongPathBuilder1 As SketchAlongPathBuilder
sketchAlongPathBuilder1 = workPart.Sketches.CreateSketchAlongPathBuilder(nullSketch)
sketchInPlaceBuilder1.PlaneOption = Sketch.PlaneOption.ExistingPlane
sketchAlongPathBuilder1.PlaneLocation.Expression.RightHandSide = "0"
Dim point1 As Point3d
point1 = curPoint.coordinates
sketchInPlaceBuilder1.PlaneOrFace.SetValue(datumPlane1, workPart.ModelingViews.WorkView, point1)
Dim nXObject1 As NXObject
nXObject1 = sketchInPlaceBuilder1.Commit()
Dim sketch1 As Sketch = CType(nXObject1, Sketch)
'Dim feature1 As Features.Feature
'feature1 = sketch1.Feature
sketch1.Activate(Sketch.ViewReorient.False)
sketchInPlaceBuilder1.Destroy()
sketchAlongPathBuilder1.Destroy()
section1.Destroy()
transform = New Transform(activeSketch.Origin, activeSketch.Orientation.Element)
CreateChord( counter, associativeLineBuilder1, transform ) 'Creates and dimensions the chord
'CreateTE() Creates and dimensions the trailing edge
'CreateTopSurfaceSpline( strDir ) Creates and constrains top surface of sketch
'CreateBotSurfaceSpline() Creates and constrains bottom surface of sketch
theSession.ActiveSketch.Deactivate(Sketch.ViewReorient.False, Sketch.UpdateLevel.Model)
End Sub
Public Sub CreateChord( ByRef counter As Integer, ByRef associativeLineBuilder1 As Features.AssociativeLineBuilder, ByRef Transform As Transform)
'------------------------------------------------------------------------------------------
' Place a line from -x, -y to +x, +y
' Points need z value?????? But each sketch, each chord is on different datum,
' at different angle
'------------------------------------------------------------------------------------------
Dim startPoint1 As Point3d = Transform.Apply( New Point3d( -33.26829028699467, -35.0000000000009, 0 ) )
Dim endPoint1 As Point3d = Transform.Apply( New Point3d( 33.0495132264591, 38.7111164380587, 0 ) )
Dim line1 As Line
line1 = workPart.Curves.CreateLine(startPoint1, endPoint1)
ActiveSketch.AddGeometry(line1, Sketch.InferConstraintsOption.InferNoConstraints)
'------------------------------------------------------------------------------------------
' Constrain curLine endpoint as point on line1
'------------------------------------------------------------------------------------------
' Dim conGeom1_1 As Sketch.ConstraintGeometry
' conGeom1_1.Geometry = line1
' conGeom1_1.PointType = Sketch.ConstraintPointType.None
' conGeom1_1.SplineDefiningPointIndex = 0
' Dim conGeom2_1 As Sketch.ConstraintGeometry
' Dim feature1 = associativeLineBuilder1.commitfeature()
' Dim associativeLine1 As Features.AssociativeLine = CType(workPart.Features.FindObject("LINE(3)"), Features.AssociativeLine)
' Dim line2 As Line = CType(associativeLine1.FindObject("CURVE 1"), Line)
' Dim endPoint as SelectPoint = associativeLineBuilder1.endpoint
' Dim curLPoint As Point3d = SelectPoint.GetValue( )
' conGeom2_1.Geometry = line2
' conGeom2_1.PointType = Sketch.ConstraintPointType.EndVertex
' conGeom2_1.SplineDefiningPointIndex = 0
' Dim help1 As Sketch.ConstraintGeometryHelp
' help1.Type = Sketch.ConstraintGeometryHelpType.Point
' help1.Point.X = curLPoint.x
' help1.Point.Y = curLPoint.y
' help1.Point.Z = curLPoint.z
' help1.Parameter = 0.0
' Dim sketchHelpedGeometricConstraint1 As SketchHelpedGeometricConstraint
' sketchHelpedGeometricConstraint1 = theSession.ActiveSketch.CreatePointOnCurveConstraint(conGeom1_1, conGeom2_1, help1)
'------------------------------------------------------------------------------------------
' Dimension length as s(counter)_Chord
'------------------------------------------------------------------------------------------
'Dim strLineLength = "S" & counter & "_Chord"
'------------------------------------------------------------------------------------------
' Dimension distance from line1 startpoint to curLine endpoint as S(counter)_YP
'------------------------------------------------------------------------------------------
'Dim strYOffset = "S" & counter & "_YP"
'------------------------------------------------------------------------------------------
' Dimension angle from line2 to y-axis as S(counter)_Twist
'------------------------------------------------------------------------------------------
'Dim strTwist = "S" & counter & "_Twist"
End Sub
I have a Journal Execution Error which states:
"SCRIPT_INVOKE
System.Reflection.TargetException:Non-Static method requires a target.
at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfor culture, Boolean SkipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invoke attr, Binder binder, Object[] parameters, CultureInvoculture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at NXOpen.Utilities.ScriptHost.Run(String[] args, Boolean withUI)
Any help would be appreciated.
RE: Programing Sketch Constraint Examples
Now I have an error, associated with this line. I have the createChord class commented out, and the following line
CODE
drops the following error:
"System.NullReferenceException: Object variable or WIth block variable not set.
at Microsoft.VisualBasic.CompilerServices.Symbols.Container..ctor(Object Instance)
at Microsoft.VisualBasic.COmpilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String Member Name, Object[] Arguments, String[]ArgumentName, Type[] TypeArguments, Boolean[] CopyBack)
at NXJournal.CreateSketch(Int322 counter, Point curPoint, DatumPlane datumPlanne1, AssociativeLineBuilder associativeLineBuilder, String_strDir) in "Temp location journal.vb:line614
When that line of code is commented out everything works, mind you the create chord is still commented out. That is copied directly from the sample code, though it was in its own Sub. I didn't do this because I needed the value to be reset each time create sketch was called.
Again any help would be appreciated.
RE: Programing Sketch Constraint Examples
www.nxjournaling.com
RE: Programing Sketch Constraint Examples
CODE
Imports System.IO
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Features
Module NXJournal
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim activeSketch = theSession.ActiveSketch
Dim ui As UI = UI.GetUI()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim session As Session = Session.GetSession()
Dim sr As StreamReader
Dim myFeats As FeatureCollection = workPart.Features
IT is defined outside of all the subroutines, I thought that would make it accessible to all of them.
RE: Programing Sketch Constraint Examples
If your journal is not intended to be run only when a sketch is active, change your module level declaration to:
CODE
www.nxjournaling.com
RE: Programing Sketch Constraint Examples
CODE
make the sketch active? This comes before the transform activation.
I am trying to create the sketch and then sketch in the program. So the Sub routine CreateSketch creates it, and then after the activate I was creating the sketch itself, using subroutines, when they were complete, the call out
CODE
Would deactivate the sketch and close it down.
Am I missing a command line? should the destroy callouts be after the sketch features are complete?
RE: Programing Sketch Constraint Examples
You can either update your variable when you activate a sketch, or you can change your code as below:
CODE
www.nxjournaling.com