A macro that changes the iPart instance of a Drawing

A macro that changes the iPart instance of a Drawing

JonnyPot
Advocate Advocate
1,515 Views
10 Replies
Message 1 of 11

A macro that changes the iPart instance of a Drawing

JonnyPot
Advocate
Advocate

Hello. I have created a iPart witch has hundreds of instances and i would like to make individual drawings for all of them. I was wondering if it's possebel to create a macro scrip that when ran on a drawing changes its iPart instance, updeits it, saves it and then repites the same actions to all the other instances in the iPart.

0 Likes
Accepted solutions (2)
1,516 Views
10 Replies
Replies (10)
Message 2 of 11

JhoelForshav
Mentor
Mentor

Hi @JonnyPot 

You can change the instance with the property ActiveMemberName in the DrawingView object.

Try creating a drawing of one of your iPart instances, and then run this macro.

Just remember to assign your desired directory for where the files should be saved in to the variable "oPath".

Sub iPartDWG()
Dim oDrawing As DrawingDocument
Set oDrawing = ThisApplication.ActiveDocument
Dim oPath As String
oPath = "C:\Jhoel\iPartDWG" 'Folder to save the drawings
Dim oView As DrawingView
For Each oView In oDrawing.ActiveSheet.DrawingViews
Dim oDoc As PartDocument
Set oDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
Dim oRow As iPartTableRow
For Each oRow In oDoc.ComponentDefinition.iPartMember.ParentFactory.TableRows
    If oView.ActiveMemberName <> oRow.MemberName Then oView.ActiveMemberName = oRow.MemberName
    Call oDrawing.Update
    Call oDrawing.SaveAs(oPath & "\" & oRow.MemberName & ".dwg", True)
Next
Next
End Sub
Message 3 of 11

JonnyPot
Advocate
Advocate

Hello. tried the Macro and i got a 2 errors. tell me if you know how to fix them
1- after it makes around 5 drawings i get a "run-time" error.

ErrorAutoDrawing.PNG

ErrorAutoDrawing2.PNG

2- dispite the "For Each oView In oDrawing.ActiveSheet.DrawingViewsIt" it only updates one of the view.

ErrorAutoDrawing3.PNG

0 Likes
Message 4 of 11

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @JonnyPot 

Sorry, I see now that I got some operations in the wrong order in the previous code. Should have tried it first!

This one however I have tried and it worked for me 🙂 Let me know if there are any issues with it 🙂

Sub iPartDWG()
Dim oDrawing As DrawingDocument
Set oDrawing = ThisApplication.ActiveDocument
Dim oPath As String
oPath = "C:\Users\hfljf\Desktop\TestFolder" 'Folder to save the drawings
Dim oView As DrawingView
Dim oDoc As Document
Dim oFactory As iPartFactory
For Each oView In oDrawing.ActiveSheet.DrawingViews
    Set oDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
    On Error GoTo trynext
    Set oFactory = oDoc.ComponentDefinition.iPartMember.ParentFactory
    Exit For
trynext:
Next
    If oFactory Is Nothing = False Then
    Dim oRow As iPartTableRow
    On Error GoTo nextView
    For Each oRow In oFactory.TableRows
        For Each oView In oDrawing.ActiveSheet.DrawingViews
        If oView.ActiveMemberName <> oRow.MemberName Then oView.ActiveMemberName = oRow.MemberName
        Call oDrawing.Update
        Do While oView.IsUpdateComplete = False
            Call ThisApplication.UserInterfaceManager.DoEvents
        Loop
nextView:
    Next
    Call oDrawing.SaveAs(oPath & "\" & oRow.MemberName & ".dwg", True)
Next
End If
End Sub
Message 5 of 11

JonnyPot
Advocate
Advocate

it works perfectly thank you very much.

Message 6 of 11

JonnyPot
Advocate
Advocate

is it possible to convert it to auto export in STEP all the instances of an iPart. thank you.

0 Likes
Message 7 of 11

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @JonnyPot 

I added a few lines to the code so that it now saves the dwg and the corresponding iPart member as a step file 🙂

Sub iPart_DWG_STP()
Dim oDrawing As DrawingDocument
Set oDrawing = ThisApplication.ActiveDocument
Dim oPath As String
oPath = "C:\Users\hfljf\Desktop\TestFolder" 'Folder to save the drawings
Dim oView As DrawingView
Dim oDoc As Document
Dim oFactory As iPartFactory
For Each oView In oDrawing.ActiveSheet.DrawingViews
    Set oDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
    On Error GoTo trynext
    Set oFactory = oDoc.ComponentDefinition.iPartMember.ParentFactory
    Exit For
trynext:
Next
    If oFactory Is Nothing = False Then
    Dim oRow As iPartTableRow
    
    For Each oRow In oFactory.TableRows
        For Each oView In oDrawing.ActiveSheet.DrawingViews
        On Error GoTo nextView
        If oView.ActiveMemberName <> oRow.MemberName Then oView.ActiveMemberName = oRow.MemberName
        Call oDrawing.Update
        Do While oView.IsUpdateComplete = False
            Call ThisApplication.UserInterfaceManager.DoEvents
        Loop
nextView:
    Next
    Call oDrawing.SaveAs(oPath & "\" & oRow.MemberName & ".dwg", True)
    Dim oMember As PartDocument
    Set oMember = ThisApplication.Documents.ItemByName(oFactory.MemberCacheDir & "\" & oRow.PartName)
    Call oMember.SaveAs(oPath & "\" & oRow.MemberName & ".stp", True)
Next
End If
End Sub
Message 8 of 11

JonnyPot
Advocate
Advocate

thank you very mutch.

0 Likes
Message 9 of 11

JonnyPot
Advocate
Advocate

Hello @JhoelForshav , sorry if i disturb you, i have a small problem with the Macro wen tryping to create drawings for iParts that contain iLogic rules. the problem being that the rules dont update, do you know any solutions, thankyou.

0 Likes
Message 10 of 11

JhoelForshav
Mentor
Mentor

@JonnyPot 

I see you have already created a new thread about this issue so I'll answer there instead 🙂

0 Likes
Message 11 of 11

JanHalus
Explorer
Explorer

Excelent solution, helped me a lot! Thanks! 🙂 I have a question tho, what do I have to change to create new sheets within the same .idw, instead of sepparate .dwg files?

0 Likes