AnsweredAssumed Answered

Create Drawings vba to vb.net convertion

Question asked by Pichaiyan Prabaharan on Jul 23, 2018
Latest reply on Jul 24, 2018 by Pichaiyan Prabaharan

I have a macro to create drawings which is working fine with vba. I need to convert that to vb.net. But its not working properly. May I know what's wrong with the codes

 

 

VBA codes

Sub main()
  Dim swApp As SldWorks.SldWorks
  Dim swModel As SldWorks.ModelDoc2
  Dim swDraw As SldWorks.DrawingDoc
  Dim swSheet As SldWorks.Sheet
  Dim swView As SldWorks.View
  Dim vSheetName, vModelName
  Dim i As Long
  Dim boolstatus As Boolean
  Dim lErrors  As Long
  Dim lWarnings As Long


  Set swApp = Application.SldWorks
  Set swDraw = swApp.ActiveDoc
  Set swModel = swDraw


  'Visit each sheet
  vSheetName = swDraw.GetSheetNames
  For i = 0 To UBound(vSheetName)
  


    'Activate and access
    swDraw.ActivateSheet vSheetName(i)
    Set swSheet = swDraw.Sheet(vSheetName(i))
    If swSheet.IsLoaded Then
      'Okay
    Else
      If MsgBox(vSheetName(i) & " is not loaded.", vbOKCancel + vbDefaultButton2) = vbCancel Then Exit Sub
    End If


    'Visit all views
    Set swView = swDraw.GetFirstView
    While Not swView Is Nothing
      Select Case swView.Type
        Case swDrawingViewTypes_e.swDrawingNamedView
          If IsEmpty(vModelName) Then


           vModelName = UserForm1.partfilepath
                       
          Else
            'Set the model in this view
            swDraw.InsertModelInPredefinedView vModelName
          End If
          'Make the sketches visible
          swView.ResetSketchVisibility
      End Select
      
      Set swView = swView.GetNextView
    Wend
  Next
  'Back to first view
  swDraw.ActivateSheet vSheetName(0)
  'Force a rebuild!
  swModel.ForceRebuild3 True
  swModel.EditRebuild3
  'Save drawing to same location as model
    Dim swIsoView As SldWorks.View
    Dim strFilePath As String


    strFilePath = Replace(vModelName, "sldprt", "SLDPRT")
    strFilePath = Replace(strFilePath, "SLDPRT", "slddrw")
    Debug.Print strFilePath
    'swModel.Extension.SaveAs strFilePath, 0, 0, Nothing, Empty, Empty
    boolstatus = swModel.SaveAs3(strFilePath, 0, 2)
    swApp.QuitDoc swModel.GetTitle
    
End Sub

 

 

VB.net Codes

 

Sub Creator()


        swApp = CreateObject("SldWorks.Application")
        Part = swApp.NewDocument(TextBox2.Text, 12, 0, 0)
        Dim swDrawing As DrawingDoc
        swDrawing = Part
        Dim swSheet As Sheet
        swSheet = swDrawing.GetCurrentSheet()
        Part = swApp.ActiveDoc
        Dim myModelView As Object
        myModelView = Part.ActiveView
        myModelView.FrameState = 1
        Part.ViewZoomtofit2()


        swApp = CreateObject("SldWorks.Application")
        swDraw = swApp.ActiveDoc


        vSheetName = swDraw.GetCurrentSheet()
        For i = 0 To UBound(vSheetName)
            swDraw.ActivateSheet(vSheetName(i))
            swSheet = swDraw.Sheet(vSheetName(i))
            swView = swDraw.GetFirstView


            While Not swView Is Nothing


                Select Case swView.Type
                    Case 7
                        If IsNothing(vModelName) Then
                            vModelName = partfilepath
                            MsgBox(vModelName)
                        Else
                            swDraw.InsertModelInPredefinedView(vModelName)
                            MsgBox(vModelName)
                        End If
                        swView.ResetSketchVisibility()


                End Select
                swView = swView.GetNextView
            End While


        Next


        swDraw.ActivateSheet(vSheetName(0))
        swDraw.EditRebuild()


        strFilePath = Replace(vModelName, "sldprt", "slddrw")
        swModel.Extension.SaveAs(strFilePath, 0, 0, Nothing, vbEmpty, vbEmpty)
        swApp.QuitDoc(swModel.GetTitle)


    End Sub

 

 

And I suspect the below particular portion is something wrong in vb.net

 

Case 7
                        If IsNothing(vModelName) Then
                            vModelName = partfilepath
                            MsgBox(vModelName)
                        Else
                            swDraw.InsertModelInPredefinedView(vModelName)
                            MsgBox(vModelName)
                        End If
                        swView.ResetSketchVisibility()

 

 

If anybody figure-out wrong stuffs, that would be grateful to me.

 

Thanks

Outcomes