I am attempting to create a sorting algorithm by creating a csys on the largest planar face of a body, and then moving it to the ABS. I have two methods I am currently testing and I can't find out why one method isn't working. I'm hoping someone can see what I'm doing wrong. Here is the base code:
For Each ThisBodyObject As NXObject In VisibleObjects
If TypeOf ThisBodyObject Is Body Then
Dim objbody As Body = ThisBodyObject
Dim longestEdge As Edge
Dim otherEdge As Edge
Dim PlanarFace As Face
Dim origin() As Double = {0, 0, 0}
Dim xVector(2) As Double
Dim yVector(2) As Double
Dim sp(2) As Double
Dim ep(2) As Double
Dim sp2(2) As Double
Dim ep2(2) As Double
Dim lineData As UFCurve.Line
LargestPlanarFace(objbody, PlanarFace)
GetLongestEdge(PlanarFace.GetEdges(), longestEdge)
lineData.start_point = sp
lineData.end_point = ep
TheUfSession.Curve.AskLineData(longestEdge.Tag, lineData)
sp = lineData.start_point
ep = lineData.end_point
TheUfSession.Vec3.Sub(ep, sp, xVector)
TheUfSession.Vec3.Copy(sp, origin)
FindPerpendicularEdge(PlanarFace.GetEdges(), sp, ep, longestEdge.Tag, otherEdge)
otherEdge.Highlight()
TheUfSession.Curve.AskLineData(otherEdge.Tag, lineData)
sp2 = lineData.start_point
ep2 = lineData.end_point
This is the code that works:
If sp2(0) = sp(0) And sp2(1) = sp(1) And sp2(2) = sp(2) Then
TheUfSession.Vec3.Sub(ep2, sp2, yVector)
Else
TheUfSession.Vec3.Sub(sp2, ep2, yVector)
End If
Dim mtx As NXOpen.Tag = NXOpen.Tag.Null
Dim newCsys As NXOpen.Tag = NXOpen.Tag.Null
Dim matrixValues(8) As Double
TheUfSession.Mtx3.Initialize(xVector, yVector, matrixValues)
TheUfSession.Csys.CreateMatrix(matrixValues, mtx)
TheUfSession.Csys.CreateCsys(origin, mtx, newCsys)
MoveCsysToCsys(Utilities.NXObjectManager.Get(newCsys), objbody)
End If
Next
End Sub
This code does not:
Dim ep3(2) As Double
TheUfSession.Vec3.Sub(ep2, sp2, ep3)
TheUfSession.Vec3.Add(sp, ep3, yVector)
Dim MoveCsys As CartesianCoordinateSystem
CreateCsys(xVector, yVector, origin, MoveCsys)
MoveCsysToCsys(MoveCsys, objBody)
End If
Next
End Sub
Sub CreateCsys(ByRef XVector() As Double, ByRef YVector() As Double, ByRef Origin() As Double, ByRef Csys1 As CartesianCoordinateSystem)
Dim OriginPt As Point3d = New Point3d(Origin(0), Origin(1), Origin(2))
Dim Coordsx As Point3d = New Point3d(XVector(0), XVector(1), XVector(2))
Dim Coordsy As Point3d = New Point3d(YVector(0), YVector(1), YVector(2))
Dim NullFeaturesFeature As Features.Feature = Nothing
Dim DatumCsysBuilder1 As Features.DatumCsysBuilder
DatumCsysBuilder1 = Workpart.Features.CreateDatumCsysBuilder(NullFeaturesFeature)
Dim Point1 As Point
Point1 = Workpart.Points.CreatePoint(OriginPt)
Dim Point2 As Point
Point2 = Workpart.Points.CreatePoint(Coordsx)
Dim Point3 As Point
Point3 = Workpart.Points.CreatePoint(Coordsy)
Dim Xform1 As Xform
Xform1 = Workpart.Xforms.CreateXform(Point1, Point2, Point3, SmartObject.UpdateOption.WithinModeling, 1.0)
Csys1 = Workpart.CoordinateSystems.CreateCoordinateSystem(Xform1, SmartObject.UpdateOption.WithinModeling)
DatumCsysBuilder1.Csys = Csys1
DatumCsysBuilder1.DisplayScaleFactor = 1.25
Dim nXObject1 As NXObject
nXObject1 = DatumCsysBuilder1.Commit()
DatumCsysBuilder1.Destroy()
End Sub
re: code doesn't work
Can you give more information about what isn't working? Do you get any error messages? If so, what are they? Does the csys get created in the 2nd code listing? Does the desired geometry move?
You may want to refer to
http://www.nxjournaling.com/content/wrangling-wcs
to see some methods of creating csys objects (the non-datum kind).
CSYS created doesn't line up
The 2nd code listing creates a csys that doesn't line up with the edges of the largest planar face or the face itself. I thought I might be using some UFVEC3 properties wrong but I tested that as well and I was inputting the correct vectors. I believe it's something within the CreateCsys() class that is being called inappropriately and I've been attempting to debug it for a day or two and haven't gotten anywhere.
I haven't worked much with xforms, I admit to have been using that code blindly off an example I found on Eng-Tips (http://www.eng-tips.com/viewthread.cfm?qid=338288) as well as when I first started tackling this problem and you helped me (http://www.eng-tips.com/viewthread.cfm?qid=341698).
DHuskic
Nx 9 VB
re: create csys
I made a small change to the CreateCsys subroutine. I add the values of the X and Y vector to the origin, translating the vector so that it passes through the origin. Otherwise, when you create the X and Y coordinate point, the values are taken as absolute coordinates (when they should be relative to the specified origin).
Sub CreateCsys(ByVal XVector() As Double, ByVal YVector() As Double, ByVal Origin() As Double, ByRef Csys1 As CartesianCoordinateSystem)
theUfSession.Vec3.Add(Origin, XVector, XVector)
theUfSession.Vec3.Add(Origin, YVector, YVector)
Dim OriginPt As Point3d = New Point3d(Origin(0), Origin(1), Origin(2))
Dim Coordsx As Point3d = New Point3d(XVector(0), XVector(1), XVector(2))
Dim Coordsy As Point3d = New Point3d(YVector(0), YVector(1), YVector(2))
Dim NullFeaturesFeature As Features.Feature = Nothing
Dim DatumCsysBuilder1 As Features.DatumCsysBuilder
DatumCsysBuilder1 = workPart.Features.CreateDatumCsysBuilder(NullFeaturesFeature)
Dim Point1 As Point
Point1 = workPart.Points.CreatePoint(OriginPt)
Dim Point2 As Point
Point2 = workPart.Points.CreatePoint(Coordsx)
Dim Point3 As Point
Point3 = workPart.Points.CreatePoint(Coordsy)
Dim Xform1 As Xform
Xform1 = workPart.Xforms.CreateXform(Point1, Point2, Point3, SmartObject.UpdateOption.WithinModeling, 1.0)
Csys1 = workPart.CoordinateSystems.CreateCoordinateSystem(Xform1, SmartObject.UpdateOption.WithinModeling)
DatumCsysBuilder1.Csys = Csys1
DatumCsysBuilder1.DisplayScaleFactor = 1.25
Dim nXObject1 As NXObject
nXObject1 = DatumCsysBuilder1.Commit()
DatumCsysBuilder1.Destroy()
End Sub