- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello
Some context to this issue.
I was having issues with the built in command below to export face to DXF.
ThisApplication.CommandManager.ControlDefinitions.Item("GeomToDXFCommand")
There is no way to specify the export version (See related posts on this below). This meant that the exported dxf would export with splines and would fail on import by other programs requiring the splines to be linearized.
iLogic/API: Provide an API for exporting a face to DXF (with options) - Autodesk Community
Setting the ACAD Version for Export Surface from Part to dxf File with the API - Autodesk Community
So, I have this rule that creates a dxf file from a selected face. It linearizes all the edges on a face and writes to a dxf file.
The problem I have is that it only works on horizontal faces, because the XY coordinates of the edge vertices are global and not transformed to the normal of the face selected.
How would I transform these points to give me the local XY coordinates from the face selected?
Sub Main() Dim oDoc As PartDocument = ThisDoc.Document Dim oCompDef = oDoc.ComponentDefinition Dim entity = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select a Face") oDoc.SelectSet.Select(entity) Dim oFace As Face Dim oTG As TransientGeometry = ThisApplication.TransientGeometry Dim oPointCol As New List(Of Double()) Try oFace = ThisApplication.ActiveDocument.SelectSet.Item(1) Catch MsgBox("A face must be selected.") End Try oDoc.SelectSet.Clear 'Dim oSketch As PlanarSketch 'oSketch = oCompDef.Sketches.Add(oFace) 'oSketch.OriginPoint = oCompDef.WorkPoints.Item(1) ' Create a new file Dim strUserProfile As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile) 'MessageBox.Show(strUserProfile, "Title") Dim fileName As String = strUserProfile & "\Downloads\FaceToDXFExample.dxf" Dim file As New System.IO.StreamWriter(fileName) SharedVariable("DXF_Data_Exists") = False ' Write DXF header file.WriteLine("0") file.WriteLine("SECTION") file.WriteLine("2") file.WriteLine("ENTITIES") For Each oEL As EdgeLoop In oFace.EdgeLoops For k = 1 To oEL.Edges.Count 'Each oEdge As Edge In oEL.Edges oEdge = oEL.Edges(k) oEdge_StopVertex = oEdge.StopVertex oEdge_StartVertex = oEdge.StartVertex oEdge_StopVertex_Coordinates = "X" & oEdge.StopVertex.Point.X & "Y" & oEdge.StopVertex.Point.Y oEdge_StartVertex_Coordinates = "X" & oEdge.StartVertex.Point.X & "Y" & oEdge.StartVertex.Point.Y ' Dim oHSet As HighlightSet = oDoc.CreateHighlightSet() ' oHSet.Color = ThisApplication.TransientObjects.CreateColor(185, 0, 0) 'Red ' oHSet.AddItem(oEdge) ' MessageBox.Show("Edge type is " & oEdge.GeometryType) ' oHSet.Clear() If k = 1 Then 'first line in loop If oEdge.GeometryType = CurveTypeEnum.kLineCurve Or oEdge.GeometryType = CurveTypeEnum.kLineSegmentCurve Then PointCollection(oPointCol, oEdge_StartVertex.Point.X, oEdge_StartVertex.Point.Y,oTG,oSketch) PointCollection(oPointCol,oEdge_StopVertex.Point.X,oEdge_StopVertex.Point.Y,oTG,oSketch) Else CalculateEdgeStrokes(oEdge,oPointCol,oTG,oSketch) End If Else 'Line is not first line in loop If oEdge.GeometryType = CurveTypeEnum.kLineCurve Or oEdge.GeometryType = CurveTypeEnum.kLineSegmentCurve Then oPreviousEdge = oEL.Edges(k - 1) oPreviousEdge_StopVertex_Coordinates = "X" & oPreviousEdge.StopVertex.Point.X & "Y" & oPreviousEdge.StopVertex.Point.Y oPreviousEdge_StartVertex_Coordinates = "X" & oPreviousEdge.StartVertex.Point.X & "Y" & oPreviousEdge.StartVertex.Point.Y If oEdge_StartVertex_Coordinates = oPreviousEdge_StopVertex_Coordinates Or oEdge_StartVertex_Coordinates = oPreviousEdge_StartVertex_Coordinates 'Good the start of the current line shares the start or stop of the previous line PointCollection(oPointCol, oEdge_StartVertex.Point.X, oEdge_StartVertex.Point.Y,oTG,oSketch) PointCollection(oPointCol,oEdge_StopVertex.Point.X,oEdge_StopVertex.Point.Y,oTG,oSketch) Else 'switch current line start/stop positions PointCollection(oPointCol,oEdge_StopVertex.Point.X,oEdge_StopVertex.Point.Y,oTG,oSketch) PointCollection(oPointCol, oEdge_StartVertex.Point.X, oEdge_StartVertex.Point.Y,oTG,oSketch) End If Else CalculateEdgeStrokes(oEdge,oPointCol,oTG,oSketch) End If End If Next WritePointsToDXF(file, oPointCol) oPointCol.Clear Next 'MessageBox.Show("Point collection contains " & oPointCol.Count, "Title") 'For i As Integer = 0 To oPointCol.Count - 1 ' oPoint = oTG.CreatePoint2d(oPointCol(i)(0), oPointCol(i)(1)) ' oSketch.SketchPoints.Add(oPoint) ' MessageBox.Show("Point Added", "Title") 'Next ' Write DXF footer file.WriteLine("0") file.WriteLine("ENDSEC") file.WriteLine("0") file.WriteLine("EOF") ' Close the file file.Close() If SharedVariable("DXF_Data_Exists") = False Then System.IO.File.Delete(fileName) End If End Sub Sub PointCollection(oPointCol As List(Of Double()), X As Double, Y As Double, oTG As TransientGeometry,oSketch As PlanarSketch) If oPointCol.Count = 0 Then oPointCol.Add({X, Y }) ' oPoint = oTG.CreatePoint2d(X, Y) ' oSketch.SketchPoints.Add(oPoint) ' MessageBox.Show("Point Added", "Title") Else intLastIndex = oPointCol.Count - 1 If oPointCol(intLastIndex)(0) = X And oPointCol(intLastIndex)(1) = Y Then ' MessageBox.Show("Points are equal - will skip") oPointCol.Add({X, Y }) Else oPointCol.Add({X, Y }) ' oPoint = oTG.CreatePoint2d(X, Y) ' oSketch.SketchPoints.Add(oPoint) ' MessageBox.Show("Point Added", "Title") End If End If Logger.Debug("X: " & X & ", Y: " & Y) End Sub Sub CalculateEdgeStrokes(oEdge As Edge,oPointCol As List(Of Double()),oTG As TransientGeometry,oSketch As PlanarSketch) Dim lVertexCount As Long Dim lSegmentCount As Long Dim adVertexCoords() As Double = {} Dim alVertexIndices() As Integer = {} oEdge.CalculateStrokes(0.01, lVertexCount, lSegmentCount, adVertexCoords, alVertexIndices) j = 1 For i = 0 To adVertexCoords.Length -1 If j = 1 Then strCoord_X = adVertexCoords(i) Else If j = 2 Then strCoord_Y = adVertexCoords(i) Else If j = 3 Then strCoord_Z = adVertexCoords(i) PointCollection(oPointCol,strCoord_X,strCoord_Y,oTG,oSketch) j = 0 'reset the counter End If j = j + 1 Next End Sub Sub WritePointsToDXF(file As System.IO.StreamWriter, oPointCol As List(Of Double())) SharedVariable("DXF_Data_Exists") = True ' Write lines to file For i = 0 To oPointCol.Count - 2 file.WriteLine("0") file.WriteLine("LINE") file.WriteLine("8") file.WriteLine("0") file.WriteLine("10") file.WriteLine(oPointCol(i)(0)) file.WriteLine("20") file.WriteLine(oPointCol(i)(1)) file.WriteLine("11") file.WriteLine(oPointCol(i+1)(0)) file.WriteLine("21") file.WriteLine(oPointCol(i+1)(1)) Next ' Add the last line to close the shape file.WriteLine("0") file.WriteLine("LINE") file.WriteLine("8") file.WriteLine("0") file.WriteLine("10") file.WriteLine(oPointCol((oPointCol.Count - 1))(0)) file.WriteLine("20") file.WriteLine(oPointCol((oPointCol.Count - 1))(1)) file.WriteLine("11") file.WriteLine(oPointCol(0)(0)) file.WriteLine("21") File.WriteLine(oPointCol(0)(1)) End Sub
Solved! Go to Solution.