1 Reply Latest reply on Jun 5, 2014 1:00 AM by Deepak Gupta

    Older macro new SW

    george trott

      I have had this macro that another guy wrote for a while. It has worked thru every upgrade till now. Basicly what the process is, select a part or parts from an assembly (either from the tree or in the view window), then click the macro button and what it is supose to do is create a drawing from my desired drawing templates with front, left, right, and iso views, Use the dims set for drawing, insert a defined bom for the part and save the drawing. If a drawing exist then a window will pop up that says drawing exist use existing drawing, yes/no? It works fine in 2013 but it is not working in 2014. the code is below. I have changed the bold lines to my current search paths for the drawing template and BOM template, and the underlined line is where I am having the issues

       

       

       

       

      Option Explicit

      Dim swApp As SldWorks.SldWorks

      Dim Part As SldWorks.ModelDoc2

      Dim SELMGR As SldWorks.SelectionMgr

      Dim swView As SldWorks.View

       

       

      Dim swdrawing As SldWorks.DrawingDoc

      Dim swBOMAnnotation As SldWorks.BomTableAnnotation

      Dim swBOMFeature As SldWorks.BomFeature

       

       

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

      Dim myView As SldWorks.View

      Dim partpath As String

      Dim VIEWMODEL As String

      Dim ERRORS As Long

      Dim WARNINGS As Long

      Dim vscale As Variant

      Dim swsheet As SldWorks.Sheet

      Dim fso

      Dim pathc As Object

      Dim titlec As Object

       

       

      Dim swasm As SldWorks.ModelDoc2

      Dim swcomp As SldWorks.Component2

      Dim i As Integer

      Dim asyp As SldWorks.ModelDoc2

      Dim vannotations

      Sub main(partpath As String)

       

       

       

       

      Set fso = CreateObject("Scripting.FileSystemObject")

      If fso.FileExists(Replace(UCase(partpath), ".SLDPRT", ".SLDDRW")) Then 'check to see if a drawing already exists in the same location as the part

      MsgBox "A File With This Name Already Exists In The Folder With This Part" + Chr(13) + "Fill Will Be Opened", vbOKOnly + vbExclamation, "STOP"

      swApp.OpenDoc6 Replace(UCase(partpath), ".SLDPRT", ".SLDDRW"), swDocDRAWING, swOpenDocOptions_LoadModel, "", ERRORS, WARNINGS

      Else

       

       

      'CREATE DRAWING

      Set Part = swApp.NewDocument("C:\Users\georget\Desktop\000-The great folder\0-USER RESOURCES\DRAWING\BARR PRT A", 12, 0.2159, 0.2794) 'CREATE NEW DRAWING WITH DRAWING TEMPLATE

      Set myView = Part.CreateDrawViewFromModelView3(partpath, "*Front", 0.075, 0.075, 0) 'INSERT VIEW

       

       

      'ADD PROJECTED VIEWS

      boolstatus = Part.ActivateView("Drawing View1")

      Set myView = Part.CreateUnfoldedViewAt3(0.15, 0.075, 0, False)

      'Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0.1131746427027, 0.1133066540541, 0, False, 0, Nothing, 0)

      boolstatus = Part.ActivateView("Drawing View1")

      Set myView = Part.CreateUnfoldedViewAt3(0.075, 0.15, 0, False)

      'Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0.1131746427027, 0.1133066540541, 0, False, 0, Nothing, 0)

      boolstatus = Part.ActivateView("Drawing View1")

      Set myView = Part.CreateUnfoldedViewAt3(0.15, 0.15, 0, False)

      Part.ClearSelection2 True

       

       

      boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0.1131746427027, 0.1133066540541, 0, False, 0, Nothing, 0)

      vannotations = Part.InsertModelAnnotations3(0, 1212424, True, False, False, False) 'ADD ANNOTATIONS TO ALL VIEWS

       

       

      Set swdrawing = swApp.ActiveDoc 'get drawing

      boolstatus = swdrawing.ActivateView("Drawing View1") 'activate view

      Set swView = swdrawing.ActiveDrawingView 'get drawing view

      swView.SetDisplayMode3 False, swHIDDEN_GREYED, False, True 'SET HLV

       

       

      boolstatus = swdrawing.ActivateView("Drawing View4") 'activate view

      Set swView = swdrawing.ActiveDrawingView 'get drawing view

      swView.SetDisplayMode3 False, swHIDDEN, False, True 'SET HLV

       

       

      ' 'not used

      Set swBOMAnnotation = swView.InsertBomTable3(True, 0.4, 0.3, swBOMConfigurationAnchor_TopRight, swBomType_PartsOnly, "", "C:\Users\georget\Desktop\000-The great folder\0-USER RESOURCES\DRAWING\BOM BARR PART", False) 'INSERT BOM ANCHORED

      Set swBOMFeature = swBOMAnnotation.BomFeature 'not shure

       

       

      vscale = swView.ScaleRatio 'get view scale

      Set swsheet = swdrawing.GetCurrentSheet 'get sheet

      swsheet.SetScale vscale(0), vscale(1), True, False 'set sheet to view scale

      swView.UseSheetScale = True 'set view to sheet scale

       

       

       

       

      Set Part = swApp.ActiveDoc 'get active part

      Part.Extension.SaveAs Replace(UCase(partpath), ".SLDPRT", ".SLDDRW"), swSaveAsCurrentVersion, swSaveAsOptions_SaveReferenced, Nothing, ERRORS, WARNINGS 'SAVE

      Part.ClearSelection2 True

      End If

      'End If

      End Sub

      Sub MAIN2()

       

       

      Set swApp = Application.SldWorks

      Set swasm = swApp.ActiveDoc

      Set SELMGR = swasm.SelectionManager

       

       

      'loop through selected components

      For i = 1 To SELMGR.GetSelectedObjectCount2(-1)

          Set swcomp = SELMGR.GetSelectedObjectsComponent3(i, -1) 'get first component

          Set asyp = swcomp.GetModelDoc2

          main (swcomp.GetPathName) 'send pathname to main

      '    main (asyp.GetPathName)

      Next i

      End Sub

      Sub MAIN3()

       

       

      Set swApp = Application.SldWorks

      Set swasm = swApp.ActiveDoc

      Set SELMGR = swasm.SelectionManager

      partpath = swasm.GetPathName

       

       

      Set fso = CreateObject("Scripting.FileSystemObject")

      If fso.FileExists(Replace(UCase(partpath), ".SLDASM", ".SLDDRW")) Then 'check to see if a drawing already exists in the same location as the part

      MsgBox "A File With This Name Already Exists In The Folder With This Part" + Chr(13) + "Fill Will Be Opened", vbOKOnly + vbExclamation, "STOP"

      swApp.OpenDoc6 Replace(UCase(partpath), ".SLDASM", ".SLDDRW"), swDocDRAWING, swOpenDocOptions_LoadModel, "", ERRORS, WARNINGS

      Else

       

       

      'CREATE DRAWING

      Set Part = swApp.NewDocument("C:\Users\georget\Desktop\000-The great folder\0-USER RESOURCES\DRAWING\BARR PRT A", 12, 0.2159, 0.2794) 'CREATE NEW DRAWING WITH DRAWING TEMPLATE

      Set myView = Part.CreateDrawViewFromModelView3(partpath, "*Front", 0.075, 0.075, 0) 'INSERT VIEW

       

       

      'ADD PROJECTED VIEWS

      boolstatus = Part.ActivateView("Drawing View1")

      Set myView = Part.CreateUnfoldedViewAt3(0.15, 0.075, 0, False)

      'Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0.1131746427027, 0.1133066540541, 0, False, 0, Nothing, 0)

      boolstatus = Part.ActivateView("Drawing View1")

      Set myView = Part.CreateUnfoldedViewAt3(0.075, 0.15, 0, False)

      'Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0.1131746427027, 0.1133066540541, 0, False, 0, Nothing, 0)

      boolstatus = Part.ActivateView("Drawing View1")

      Set myView = Part.CreateUnfoldedViewAt3(0.15, 0.15, 0, False)

      Part.ClearSelection2 True

       

       

      boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0.1131746427027, 0.1133066540541, 0, False, 0, Nothing, 0)

      vannotations = Part.InsertModelAnnotations3(0, 1212424, True, False, False, False) 'ADD ANNOTATIONS TO ALL VIEWS

       

       

      Set swdrawing = swApp.ActiveDoc 'get drawing

      boolstatus = swdrawing.ActivateView("Drawing View1") 'activate view

      Set swView = swdrawing.ActiveDrawingView 'get drawing view

      swView.SetDisplayMode3 False, swHIDDEN_GREYED, False, True 'SET HLV

       

       

      boolstatus = swdrawing.ActivateView("Drawing View4") 'activate view

      Set swView = swdrawing.ActiveDrawingView 'get drawing view

      swView.SetDisplayMode3 False, swHIDDEN, False, True 'SET HLV

       

       

      Set swBOMAnnotation = swView.InsertBomTable3(True, 0.4, 0.3, swBOMConfigurationAnchor_TopRight, swBomType_PartsOnly, "", "C:\Users\georget\Desktop\000-The great folder\0-USER RESOURCES\DRAWING\BOM BARR PART", False) 'INSERT BOM ANCHORED

      Set swBOMFeature = swBOMAnnotation.BomFeature 'not shure

       

       

      vscale = swView.ScaleRatio 'get view scale

      Set swsheet = swdrawing.GetCurrentSheet 'get sheet

      swsheet.SetScale vscale(0), vscale(1), True, False 'set sheet to view scale

      swView.UseSheetScale = True 'set view to sheet scale

       

       

      Set Part = swApp.ActiveDoc 'get active part

      Part.Extension.SaveAs Replace(UCase(partpath), ".SLDASM", ".SLDDRW"), swSaveAsCurrentVersion, swSaveAsOptions_SaveReferenced, Nothing, ERRORS, WARNINGS 'SAVE

      Part.ClearSelection2 True

      End If

      'End If

       

       

      End Sub

       

       

       

       

       

       

       

      Please help.

        • Re: Older macro new SW
          Deepak Gupta

          Looks like you have missed out the extensions in these lines when changing the paths.

          'CREATE DRAWING

          Set Part = swApp.NewDocument("C:\Users\georget\Desktop\000-The great folder\0-USER RESOURCES\DRAWING\BARR PRT A", 12, 0.2159, 0.2794) 'CREATE NEW DRAWING WITH DRAWING TEMPLATE

           

           

          Set swBOMAnnotation = swView.InsertBomTable3(True, 0.4, 0.3, swBOMConfigurationAnchor_TopRight, swBomType_PartsOnly, "", "C:\Users\georget\Desktop\000-The great folder\0-USER RESOURCES\DRAWING\BOM BARR PART", False) 'INSERT BOM ANCHORED

           

          Set Part = swApp.NewDocument("C:\Users\georget\Desktop\000-The great folder\0-USER RESOURCES\DRAWING\BARR PRT A.drwdot", 12, 0.2159, 0.2794) 'CREATE NEW DRAWING WITH DRAWING TEMPLATE

           

          Set swBOMAnnotation = swView.InsertBomTable3(True, 0.4, 0.3, swBOMConfigurationAnchor_TopRight, swBomType_PartsOnly, "", "C:\Users\georget\Desktop\000-The great folder\0-USER RESOURCES\DRAWING\BOM BARR PART.sldbomtbt", False) 'INSERT BOM ANCHORED