1 Reply Latest reply on Feb 9, 2019 6:30 PM by Amen Allah Jlili

    Custom Model View in Drawing - SaveAs DXF

    Michael Corriveau-Cote

      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)
      NewTab(FileName)
      NewTab(FileName2)
      

       

       

      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
      Application.DoEvents()
      'swApp.ActiveDoc.NameView(ViewName)
      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.Activate()
      swModelView.DisplayMode = swDisplayMode_e.swHIDDEN
      swApp.ActiveDoc.ViewZoomtofit2()
      Application.DoEvents()
      swApp.ActiveDoc.DeleteNamedView(ViewName)
      swApp.ActiveDoc.NameView(ViewName)
      Application.DoEvents()
      
      
      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
      MyActiveAsmDoc.ForceRebuild3(True)
      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
      Try
      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
      Try
      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
      
      
      Try
      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
      MsgBox(ex.Message)
      End Try
      
      
      swView.SetDisplayMode3(False, DisplayMode, False, False)
      'swView.SetDisplayMode3(False, swDisplayMode_e.swWIREFRAME, False, False)
      swView.SetDisplayTangentEdges2(swDisplayTangentEdges_e.swTangentEdgesHidden)
      Application.DoEvents()
      
      
      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)
      
      
      
      
      
      
      MyActiveDoc.ForceRebuild3(True)
      
      
      Dim SaveSuccess As Boolean = MyActiveDoc.Extension.SaveAs2(FileName, swSaveAsVersion_e.swSaveAsCurrentVersion, Nothing, Nothing, "", False, errors, warnings)
      swApp.CloseDoc(MyActiveDoc.GetPathName)
      If Not SaveSuccess Then Return ""
      Return FileName
      End Function
      

       

      DisplayMode Wireframe :

       

      DisplayMode Hidden :

      When you do it manually :