Creating sketch on a user selected plane (or face) and sketch origin on a user selected point

Hi,

I have the following code that creates an sketch in a predefined sketch plane and point. But I would like user to select the plane and point (as sketch origin point). What is the best way to define the axis and point locations, so that the arcs, circles or lines follow the same direction and doesn't get distorted for changing planes?

Imports System
Imports NXOpen
Imports NXOpen.Selection
Imports NXOpen.UF
Imports NXOpenUI

Module NXJournal

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

Sub Main(ByVal args() As String)

Dim origin As New NXOpen.Point3d(0, 0, 0)
Dim axisX As New NXOpen.Point3d(1, 0, 0)
Dim wcsMatrix As NXOpen.Matrix3x3 = workPart.WCS.CoordinateSystem.Orientation.Element
Dim sketchPlane As NXOpen.DatumPlane = workPart.Datums.CreateFixedDatumPlane(origin, wcsMatrix)
Dim horizAxis As NXOpen.DatumAxis = workPart.Datums.CreateFixedDatumAxis(origin, axisX)

Dim sketchBuilder As NXOpen.SketchInPlaceBuilder
sketchBuilder = workPart.Sketches.CreateNewSketchInPlaceBuilder(Nothing)

sketchBuilder.PlaneOrFace.Value = sketchPlane
sketchBuilder.Axis.Value = horizAxis
sketchBuilder.SketchOrigin = workPart.Points.CreatePoint(origin)

sketchBuilder.PlaneOption = NXOpen.Sketch.PlaneOption.Inferred

Dim bridgeSketch As NXOpen.Sketch = sketchBuilder.Commit

sketchBuilder.Destroy()

bridgeSketch.Activate(NXOpen.Sketch.ViewReorient.False)

Dim p0 As New NXOpen.Point3d(2, 0, 0) ' Start point
Dim p1 As New NXOpen.Point3d(0, 0, 0) ' End point
Dim pm As New NXOpen.Point3d(1, 1, 0) ' Interior point
Dim gotFlipped As Boolean = False
Dim bridge As NXOpen.Arc = workPart.Curves.CreateArc(p0, pm, p1, False, gotFlipped)

theSession.ActiveSketch.AddGeometry(bridge,
NXOpen.Sketch.InferConstraintsOption.InferNoConstraints)

Dim arcPt0 As New NXOpen.Sketch.ConstraintGeometry With {
.Geometry = bridge,
.PointType = NXOpen.Sketch.ConstraintPointType.StartVertex,
.SplineDefiningPointIndex = 0
}

Dim pt0 As New NXOpen.Sketch.ConstraintGeometry With {
.Geometry = workPart.Points.CreatePoint(p0),
.PointType = NXOpen.Sketch.ConstraintPointType.None,
.SplineDefiningPointIndex = 0
}

theSession.ActiveSketch.CreateCoincidentConstraint(arcPt0, pt0)

Dim arcPt1 As New NXOpen.Sketch.ConstraintGeometry With {
.Geometry = bridge,
.PointType = NXOpen.Sketch.ConstraintPointType.EndVertex,
.SplineDefiningPointIndex = 0
}

Dim pt1 As New NXOpen.Sketch.ConstraintGeometry With {
.Geometry = workPart.Points.CreatePoint(p1),
.PointType = NXOpen.Sketch.ConstraintPointType.None,
.SplineDefiningPointIndex = 0
}

theSession.ActiveSketch.CreateCoincidentConstraint(arcPt1, pt1)

Dim length As NXOpen.Expression = workPart.Expressions.CreateExpression("Number", "length = 2.5")
Dim perimeter As NXOpen.Curve() = {bridge}
theSession.ActiveSketch.CreatePerimeterDimension(perimeter, origin, length)

theSession.ActiveSketch.LocalUpdate
theSession.ActiveSketch.Deactivate(NXOpen.Sketch.ViewReorient.False, NXOpen.Sketch.UpdateLevel.Model)

End Sub
End Module

I highly recommend creating a datum csys (or use an existing one) for sketches. Use the XY plane for the sketch, the X-axis for the horizontal reference, and the datum point for the origin. The user can re-orient the datum csys as desired without ruining the sketch.

Does this help?

First of all, there is a rettach issue if you try to reattach the XY plane sketch to a plane or face parallel to YZ (since X axis was the reference axis)

secondly, no matter what plane(of a body) I select, as sketchPlane,the sketch will be placed still on the origin, which is still fixed defined by the curve origin (not user defined)

thirdly, still trying to figure out a way to create a relative point based on user selected point, so that all the sketch objects will be together even if the origin will be placed on user defined origin...

Regards,
MFJ

Our initial plan was to use reuse library to import profiles as an sketch. But due to some access issues, we can't use the 'Reuse Library'. So only option we have left is to imitate the same way reuse 2D section works.... let user select the face of a body or a plane, select a origin point on that surface and place the sketch right there. The problem I am having is in the code above, we are already fixing the axis and the plane as shown below:


Dim origin As New Point3d(0, 0, 0)
Dim axisX As New NXOpen.Point3d(1, 0, 0)
Dim wcsMatrix As NXOpen.Matrix3x3 = workPart.WCS.CoordinateSystem.Orientation.Element
Dim sketchPlane As NXOpen.DatumPlane = workPart.Datums.CreateFixedDatumPlane(origin, wcsMatrix)
Dim horizAxis As NXOpen.DatumAxis = workPart.Datums.CreateFixedDatumAxis(origin, axisX)

Points are also not relative


Dim p0 As New NXOpen.Point3d(2, 0, 0) ' Start point
Dim p1 As New NXOpen.Point3d(0, 0, 0) ' End point

Is there any way to define the axis, plane and points can be relative to the user defined surface and point?

Regards,
MFJ

An alternative way could be what you are suggesting - automating the Reorienting the sketch by user selection of plane and point. Any suggestion on this part, if not initial idea would be appreciated. Thanks

Regards,
MFJ