Hello,
I'm working on some code where I am filtering sketches that are designated for my CNC, looping through the sketch segments, obtaining sketch points from each segment, and needing to create a list of these coordinates so that I can recreate them later in the program in my nesting software.
Following along with the API examples, I am able to get the correct coordinates comparing against the model transform, but the issue is that I am needing this in relation to a custom coordinate system I've placed in the model. Based on the model, the orientation can change file to file on which face of the part needs to be facing "up". If that all makes sense.
I am able to get the transform object for the coordinate system using the API help, but none of the coordinates are coming out correct. Can someone point me in the right direction of where I am hosing this up? This is my first go 'round with transforms, I have been trying to research online from explanations people have provided, but it is a little difficult to wrap my head around...
Any help would be greatly appreciated. Thanks!
Public Sub ConvertUserUnits(ByRef dPt() As Double, ByRef swModel As ModelDoc2)
If swModel Is Nothing Then Exit Sub
If dPt.Length < 3 Then Exit Sub
Dim swUserUnits As UserUnit = swModel.GetUserUnit(swUserUnitsType_e.swLengthUnit)
If swUserUnits IsNot Nothing Then
Dim dConvFactor As Double = swUserUnits.GetConversionFactor
dPt(0) = dPt(0) * dConvFactor
dPt(1) = dPt(1) * dConvFactor
dPt(2) = dPt(2) * dConvFactor
End If
End Sub
Public Function TransformSketchPointToCncSpace(ByRef swApp As SldWorks, ByRef swModel As ModelDoc2, ByRef swSketch As Sketch, ByRef swSketchPt As SketchPoint) As MathPoint
Dim TheAnswer As MathPoint = Nothing
Try
If swModel Is Nothing Then
TheAnswer = Nothing
Return TheAnswer
Exit Function
End If
If swSketch Is Nothing Then
TheAnswer = Nothing
Return TheAnswer
Exit Function
End If
If swSketchPt Is Nothing Then
TheAnswer = Nothing
Return TheAnswer
Exit Function
End If
Dim swMathUtil As MathUtility = Nothing
Dim dPt(2) As Double
Dim oPt As Object
Dim swCncTransform As MathTransform = Nothing
Dim swMathPt As MathPoint = Nothing
Dim sCoordName As String = ""
dPt(0) = swSketchPt.X
dPt(1) = swSketchPt.Y
dPt(2) = swSketchPt.Z
oPt = dPt
swMathUtil = swApp.GetMathUtility
sCoordName = GetCncCoordSysName(swModel)
If sCoordName = "" Then sCoordName = swModel.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swFileSaveAsCoordinateSystem)
swCncTransform = swModel.Extension.GetCoordinateSystemTransformByName(sCoordName)
If swCncTransform IsNot Nothing Then
swCncTransform = TryCast(swCncTransform.Inverse, MathTransform)
swMathPt = TryCast(swMathUtil.CreatePoint(oPt), MathPoint)
If swMathPt IsNot Nothing Then
TheAnswer = TryCast(swMathPt.MultiplyTransform(swCncTransform), MathPoint)
End If
End If
Catch ex As Exception
Debug.WriteLine(ex)
End Try
Return TheAnswer
End Function
Public Sub ProcessSketchLine(ByRef swApp As SldWorks, ByRef swModel As ModelDoc2, ByRef swSketch As Sketch, ByRef swSketchLine As SketchLine)
Try
If swApp Is Nothing Then Exit Sub
If swModel Is Nothing Then Exit Sub
If swSketch Is Nothing Then Exit Sub
If swSketchLine Is Nothing Then Exit Sub
Dim swStartPt As SketchPoint = TryCast(swSketchLine.GetStartPoint2, SketchPoint)
If swStartPt Is Nothing Then Exit Sub
Dim swEndPt As SketchPoint = TryCast(swSketchLine.GetEndPoint2, SketchPoint)
If swEndPt Is Nothing Then Exit Sub
'Dim swStartCncPt As MathPoint = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swStartPt)
'Dim swEndCncPt As MathPoint = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swEndPt)
Dim swStartCncPt As MathPoint = TransformSketchPointToCncSpace(swApp, swModel, swSketch, swStartPt)
Dim swEndCncPt As MathPoint = TransformSketchPointToCncSpace(swApp, swModel, swSketch, swEndPt)
If swStartCncPt IsNot Nothing And swEndCncPt IsNot Nothing Then
Dim dPt(2) As Double
dPt(0) = swStartPt.X
dPt(1) = swStartPt.Y
dPt(2) = swStartPt.Z
ConvertUserUnits(dPt, swModel)
AddStringToLog("Converted SketchLine for swStartPt: " + CStr(dPt(0)) + ", " + CStr(dPt(1)) + ", " + CStr(dPt(2)))
dPt(0) = swEndPt.X
dPt(1) = swEndPt.Y
dPt(2) = swEndPt.Z
ConvertUserUnits(dPt, swModel)
AddStringToLog("Converted SketchLine for swEndPt: " + CStr(dPt(0)) + ", " + CStr(dPt(1)) + ", " + CStr(dPt(2)))
dPt(0) = swStartCncPt.ArrayData(0)
dPt(1) = swStartCncPt.ArrayData(1)
dPt(2) = swStartCncPt.ArrayData(2)
ConvertUserUnits(dPt, swModel)
AddStringToLog("Converted SketchLine for swStartCncPt: " + CStr(dPt(0)) + ", " + CStr(dPt(1)) + ", " + CStr(dPt(2)))
dPt(0) = swEndCncPt.ArrayData(0)
dPt(1) = swEndCncPt.ArrayData(1)
dPt(2) = swEndCncPt.ArrayData(2)
ConvertUserUnits(dPt, swModel)
AddStringToLog("Converted SketchLine for swEndCncPt: " + CStr(dPt(0)) + ", " + CStr(dPt(1)) + ", " + CStr(dPt(2)))
End If
Catch ex As Exception
Debug.WriteLine(ex)
End Try
End Sub
