AnsweredAssumed Answered

Custom Model View in Drawing - SaveAs DXF

Question asked by Michael Corriveau-Cote on Feb 8, 2019
Latest reply on Feb 9, 2019 by Amen Allah Jlili

Hey everybody,


I've been stuck with this problem for quite a while. For some reason, I can get my custom model view in a drawing and save it as DXF in WIREFRAME but it doesnt work when I switch to HIDDEN. I mean it works but the resulting file is empty. It works good manually but doesn't have the same behavior through APIs.


Main :

DirectCast(swApp.ActiveDoc, sldworks.ModelDoc2).ClearSelection2(True)

Dim ViewName As String = CreateUcsView()

Dim FileName As String = CreateDxfFromView(ViewName, swDisplayMode_e.swHIDDEN)
Dim FileName2 As String = CreateDxfFromView(ViewName, swDisplayMode_e.swWIREFRAME)



Create View from Custom UCS :

Private Function CreateUcsView() As String
Dim currDoc As sldworks.ModelDoc2 = swApp.ActiveDoc
Dim ViewName As String = "ProgCNCView"
Dim swViewTrans As sldworks.MathTransform = swApp.ActiveDoc.Extension.GetCoordinateSystemTransformByName(GetProgUCS().Name)
Dim swModelView As sldworks.ModelView = swApp.ActiveDoc.ActiveView
Dim swViewTransInverted As sldworks.MathTransform = swViewTrans.Inverse

'swModelView.Orientation3 = swViewTrans.Inverse

Dim swMathUtility As sldworks.MathUtility = swApp.GetMathUtility()
Dim XVector As sldworks.MathVector = swMathUtility.CreateVector(New Double() {Round(swViewTransInverted.ArrayData(0)), Round(swViewTransInverted.ArrayData(1)), Round(swViewTransInverted.ArrayData(2))})
Dim YVector As sldworks.MathVector = swMathUtility.CreateVector(New Double() {Round(swViewTransInverted.ArrayData(3)), Round(swViewTransInverted.ArrayData(4)), Round(swViewTransInverted.ArrayData(5))})
Dim ZVector As sldworks.MathVector = swMathUtility.CreateVector(New Double() {Round(swViewTransInverted.ArrayData(6)), Round(swViewTransInverted.ArrayData(7)), Round(swViewTransInverted.ArrayData(8))})
Dim TransformVector As sldworks.MathVector = swMathUtility.CreateVector(New Double() {Round(swViewTransInverted.ArrayData(9)), Round(swViewTransInverted.ArrayData(10)), Round(swViewTransInverted.ArrayData(11))})
Dim ScaleValue As Double = 1
swViewTransInverted.SetData(XVector, YVector, ZVector, TransformVector, ScaleValue)

Dim InvertYAndZ As sldworks.MathTransform = swMathUtility.CreateTransform(New Double() {1, 0, 0,
0, 0, 1,
0, 1, 0,
0, 0, 0, 1,
0, 0, 0})
swModelView.Orientation3 = swViewTransInverted.Multiply(InvertYAndZ)
swModelView.DisplayMode = swDisplayMode_e.swHIDDEN

Return ViewName
End Function


Export to DXF :

Private Function CreateDxfFromView(ViewName As String, Optional DisplayMode As swDisplayMode_e = swDisplayMode_e.swHIDDEN) As String

Dim FileName As String = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".dxf"
Dim errors As Integer
Dim warnings As Integer

Dim MyActiveAsmDoc As sldworks.ModelDoc2 = swApp.ActiveDoc
Dim vModelViewNames As Object
vModelViewNames = MyActiveAsmDoc.GetModelViewNames()

Debug.Print(" ModelViewCount = " & MyActiveAsmDoc.GetModelViewCount)
For i = 0 To UBound(vModelViewNames)
Debug.Print(" " & vModelViewNames(i))
Next i

Dim MyDrawingDoc As sldworks.DrawingDoc
MyDrawingDoc = swApp.NewDocument(MyConfig.ConfigApplications.WillyEmptyDrawingTemplate, swDwgPaperSizes_e.swDwgPaperA4size, 0, 0)
Catch ex As Exception
Throw New Exception("Aucun modèle par défaut n'a été sélectionné dans les options de Solidworks. Veuillez les sélectionner (Outils->Options->Modèles par défaut). ")
End Try

Dim CurrentSheet As sldworks.Sheet
CurrentSheet = MyDrawingDoc.GetCurrentSheet
Catch ex As Exception
Throw New Exception("Il y a un problème avec un des Fichiers de Template de Solidworks")
End Try

Dim SizeX As Double = 0
Dim SizeY As Double = 0
CurrentSheet.GetSize(SizeX, SizeY)

Dim swView As sldworks.View = MyDrawingDoc.CreateDrawViewFromModelView3(MyActiveAsmDoc.GetPathName, ViewName, SizeX / 2, SizeY / 2, 0)
Dim AllView As sldworks.View = swView

While Not AllView Is Nothing

Debug.Print("View Name: " & AllView.GetName2 & vbNewLine &
"Orientation: " & AllView.GetOrientationName & vbNewLine)

AllView = AllView.GetNextView()

End While

Catch ex As Exception
End Try

swView.SetDisplayMode3(False, DisplayMode, False, False)
'swView.SetDisplayMode3(False, swDisplayMode_e.swWIREFRAME, False, False)

Dim MyActiveDoc As sldworks.ModelDoc2 = swApp.ActiveDoc

Dim AcadVersion As swDxfFormat_e = swApp.GetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swDxfVersion)
Dim FontType As String = swApp.GetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swDxfOutputFonts)
Dim LineStyles As String = swApp.GetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swDxfOutputLineStyles)
Dim CustomMap As Boolean = swApp.GetUserPreferenceToggle(swUserPreferenceToggle_e.swDxfMapping)
Dim ScaleOutput As Boolean = swApp.GetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swDxfOutputNoScale)
'Dim ScaleBase As Integer = swApp.NotAvailableInAPI
'Dim ScaleWarn As Boolean = swApp.NotAvailableInAPI
Dim EndPointMerge As Boolean = swApp.GetUserPreferenceToggle(swUserPreferenceToggle_e.swDxfEndPointMerge)
Dim SplineExport As Boolean = swApp.GetUserPreferenceToggle(swUserPreferenceToggle_e.swDxfExportSplinesAsSplines)
Dim MultiDraw As swDxfMultisheet_e = swApp.GetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swDxfMultiSheetOption)
Dim MultiDraw_PaperSpace As Boolean = swApp.GetUserPreferenceToggle(swUserPreferenceToggle_e.swDxfExportAllSheetsToPaperSpace)


Dim SaveSuccess As Boolean = MyActiveDoc.Extension.SaveAs2(FileName, swSaveAsVersion_e.swSaveAsCurrentVersion, Nothing, Nothing, "", False, errors, warnings)
If Not SaveSuccess Then Return ""
Return FileName
End Function


DisplayMode Wireframe :


DisplayMode Hidden :

When you do it manually :