Hello, I was wondering if anyone could help me with this. I have an assembly that comprises of 12 parts. I am trying to build a macro that would create 12 sheets in one drawing and then create a standard 3 view of each part, in the corresponding sheet. So far I have managed to create the sheets with a simple loop and then traverse each sheet. What I cannot understand is how to get the name of each part in order to create the views. The 12 sheets are named Part1, Part2,..., Part12. The parts though, don't follow the same name pattern.
Dim swapp As SldWorks.SldWorks
Dim swmodel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim TemplateName As String
Sub main()
Set swapp = Application.SldWorks
Set swmodel = swapp.ActiveDoc
TemplateName = "C:\Templates\Drawing.DRWDOT"
Set swDraw = swapp.NewDocument(TemplateName, swDwgPaperA3size, 0, 0)
Dim retval As Boolean
' Loop for creating multiple Sheets
Dim i As Integer
Dim SheetName As Variant
'Dim PartName As Variant
'PartName = "C:\MyParts\Upper_Link.SLDPRT" ' Example name of a part
For i = 1 To 12
SheetName = "Part" & i
swDraw.NewSheet3 SheetName, swDwgPaperA3size, swDwgTemplateA3size, 1, 5, False, "", 0, 0, ""
Next i
For i = 1 To 12
swDraw.ActivateSheet ("Part" & i)
'retval = swDraw.Create3rdAngleViews2(PartName) ' Creates views of the same part to all 12 sheets
Next i
End Sub
Wayyyyyyyyyyyyyy before you add the sheets:
- Using SldWorks.OpenDoc method, open the assembly inside a ModelDoc2
- Define an AssemblyDoc object inside which you cast the ModelDoc2 object
- Use the method AssemblyDoc.GetComponents to get a variant of component2.
- Define a collection object and call it (for example) ComponentsFilePaths
- Traverse the component2 variant and use the collection:add method to add component2.GetPathName (Now you have a collection of file paths!)
- Close the ModelDoc2 of the assembly using ModelDoc2.Close
- Remove duplicates
Back to the drawing doc now:
- Traverse the ComponentsFilePaths collection, add new sheet then use swDraw.Create3rdAngleViews2(Collection(i)) to insert the views
Help pointers:
- When you hear traversing, often times, people are refering to for each loops.
Example: Traversing a component2 variant called Components
- When you're inserting sheets, you might want to use the filename as the sheet's name.
If the filename is not meaningful, consider creating a class with two fields: name and filepath. You add whatever customproperty from the component2 as the name and the file path as seen before. After that you add the class objects to the collection. If you choose to use the filename. Use the split function to get the filename from the path.
Cheers