Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programing Sketch Constraint Examples 1

Status
Not open for further replies.

Twullf

Mechanical
Jan 24, 2012
196
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.
 
Replies continue below

Recommended for you

In addition, this is the simple code to lay down a line on the sketch:

Code:
  'Place a line 
         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.
 
If you are running NX 7.5, there are at least 2 examples in the UGOPEN directory, DrawText and SketchShape. I don't know if these were included in earlier NX releases, but should be in 7.5 or later.

 
Alright I am using, aka TRYING to use, the transform class from the SketchShape Sample.

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:
Public Sub CreateSketch( ByRef counter As Integer, ByRef curPoint As Point, ByRef datumPlane1 as DatumPlane, ByRef associativeLineBuilder1 As Features.AssociativeLineBuilder, byRef strDir as String )

       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.
 
I managed to fix that error. I was misusing the class feature, and had attempted to put in another class for the rest of the subroutines, assuming, incorrectly, that I needed to enclose the remaining subs to associate them since I already had one class. Once removed that error went away.

Now I have an error, associated with this line. I have the createChord class commented out, and the following line
Code:
transform = New Transform(activeSketch.Origin, activeSketch.Orientation.Element)

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.
 
It is not obvious from the code that you posted how and where activeSketch is defined. I suspect it is a variable scope issue; ie, your subroutine does not have access to the variable. You might try passing in the active sketch as a parameter to your sub.

 
Code:
Imports System
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.
 
Is there a sketch active when you start the journal? If not, activeSketch might be nothing; which would explain the object variable not set error you are seeing.

If your journal is not intended to be run only when a sketch is active, change your module level declaration to:
Code:
Dim activeSketch as Sketch = Nothing
and assign it after you create/activate a sketch.

 
wouldn't the code:

Code:
sketch1.Activate(Sketch.ViewReorient.False)

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:
theSession.ActiveSketch.Deactivate(Sketch.ViewReorient.False, Sketch.UpdateLevel.Model)

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?
 
The activeSketch variable you created is a snapshot in time of theSession.ActiveSketch. At the beginning of the journal, it will either be set to Nothing, or to the active sketch depending on the state of NX when the journal is launched. When your code activates a sketch, theSession.ActiveSketch will update to reflect this change; however, your variable will not change unless you tell it to. The variable is a copy of theSession.ActiveSketch, not a live link to it.

You can either update your variable when you activate a sketch, or you can change your code as below:
Code:
transform = New Transform([highlight]theSession.[/highlight]ActiveSketch.Origin, [highlight]theSession.[/highlight]ActiveSketch.Orientation.Element)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor