Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VBA - create drawing of each part of Assembly

22 REPLIES 22
SOLVED
Reply
Message 1 of 23
sam
Advocate
4147 Views, 22 Replies

VBA - create drawing of each part of Assembly

Hi All, 

my code below to create drawings from assembly is giving error and though i tried hard I haven't been able to fix or find out what is wrong with it. 

 

Sub btnGenerateDrawing_Click()

' Set reference to active document.
' This assumes the active document is an assembly
Dim oAssemblyDoc As Inventor.AssemblyDocument
Set oAssemblyDoc = ThisApplication.ActiveDocument
' Get assembly component definition
Dim oCompDef As Inventor.ComponentDefinition
Set oCompDef = oAssemblyDoc.ComponentDefinition
' Get all occurrences from component definition for Assembly document
Dim oCompOcc As ComponentOccurrence
For Each oCompOcc In oCompDef.Occurrences
'------ Locating the Template ------'
strLocation = ThisApplication.FileOptions.TemplatesPath
Set oDrgDoc = ThisApplication.Documents.Add(kDrawingDocumentObject, strLocation & "MasterBeam.idw")
Dim oSheet As Sheet
Set oSheet = oDrgDoc.Sheets.Item(1)

Dim oPoint1 As Point2d
Set oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(15#, 25#)

Dim oPoint2 As Point2d
Set oPoint2 = ThisApplication.TransientGeometry.CreatePoint2d(15#, 15#)

Dim oView1 As DrawingView
Set oView1 = oSheet.DrawingViews.AddBaseView(oCompOcc, oPoint1, 0.1, kBottomViewOrientation, kHiddenLineDrawingViewStyle)

 

It opens up the template but when it reaches on the line highlighted red it gives error of type mismatch. I have tried a lot to look around on this and other forums but couldn't find out what is wrong.

 

best regards, 

sam

22 REPLIES 22
Message 2 of 23
JhoelForshav
in reply to: sam

Hi @sam 

oCompOcc is of type ComponentOccurrence, not document. Try changing it to this:

Set oView1 = oSheet.DrawingViews.AddBaseView(oCompOcc.Definition.Document, oPoint1, 0.1, kBottomViewOrientation, kHiddenLineDrawingViewStyle)
Message 3 of 23
sam
Advocate
in reply to: JhoelForshav

@JhoelForshav Thank you it fixed the problem that I was facing. 

New problem that I am facing now (which I didn't envision at the beginning) is it is created drawings for each occurrence of same part. 

 

regards, 

Sam

 

Message 4 of 23
JhoelForshav
in reply to: sam

I thought about that too. Try this

Sub btnGenerateDrawing_Click()
' Set reference to active document.
' This assumes the active document is an assembly
Dim oAssemblyDoc As Inventor.AssemblyDocument
Set oAssemblyDoc = ThisApplication.ActiveDocument
Dim oDoc As Document

For Each oDoc In oAssemblyDoc.AllReferencedDocuments
    If oDoc.DocumentType = kPartDocumentObject Then
        If oAssemblyDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count > 0 Then
            strLocation = ThisApplication.FileOptions.TemplatesPath
            Set oDrgDoc = ThisApplication.Documents.Add(kDrawingDocumentObject, strLocation & "MasterBeam.idw")
            Dim oSheet As Sheet
            Set oSheet = oDrgDoc.Sheets.Item(1)

            Dim oPoint1 As Point2d
            Set oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(15#, 25#)

            Dim oPoint2 As Point2d
            Set oPoint2 = ThisApplication.TransientGeometry.CreatePoint2d(15#, 15#)

            Dim oView1 As DrawingView
            Set oView1 = oSheet.DrawingViews.AddBaseView(oDoc, oPoint1, 0.1, kBottomViewOrientation, kHiddenLineDrawingViewStyle)
        End If
    End If
Next
End Sub
Message 5 of 23
JhoelForshav
in reply to: JhoelForshav

Also, if you dont want drawings from parts within subassemblies, but only in the top level assembly you can change:

For Each oDoc In oAssemblyDoc.AllReferencedDocuments

To

For Each oDoc In oAssemblyDoc.ReferencedDocuments

Message 6 of 23
sam
Advocate
in reply to: JhoelForshav

Thank you so much for prompt and right help. 

 

regards, 

Sam

Message 7 of 23
sam
Advocate
in reply to: JhoelForshav


@JhoelForshav wrote:

Also, if you dont want drawings from parts within subassemblies, but only in the top level assembly you can change:

For Each oDoc In oAssemblyDoc.AllReferencedDocuments

To

For Each oDoc In oAssemblyDoc.ReferencedDocuments


Thanks for this again.

I will keep learning and will try to add some more into code.

  • rule should be able to generate drawings whether part or assembly is opened. 
  • if part or assembly is not opened, instead of giving error it should prompt to open one of those. 
  • If drawing for each (or any) part is created already again it should give prompt. 

I will try to do as much as I can but I might bug you if stuck somewhere. 

 

regards, 

sam

Message 8 of 23
Anonymous
in reply to: JhoelForshav

Hello.

Why do I have this error? Im using Inventor 2020. 

001.JPG

 
Message 9 of 23
sam
Advocate
in reply to: Anonymous

Hi @Anonymous ,

the code that you have copied to in logic editor is intended to run in VBA editor. There is not much difference though. If you want to run as iLgoic remove “set” and try again. 

 

Message 10 of 23
sam
Advocate
in reply to: Anonymous

@Anonymous  Please read this thread to understand the different and how to use the code interchangeably. 

https://forums.autodesk.com/t5/inventor-customization/ilogic-vs-vba-similarities-differences-and-advantages/td-p/8938983

regards, 

sam

Message 11 of 23
sam
Advocate
in reply to: JhoelForshav

@JhoelForshav I am struggling to add how I could make the could work if a part is open. Only way I could make it work was repeating the whole code all over again. Please have a look whenever you can. 

Thanks, 

Regards, 

Sam


@JhoelForshav wrote:

I thought about that too. Try this

 

Sub btnGenerateDrawing_Click()
' Set reference to active document.
' This assumes the active document is an assembly
Dim oAssemblyDoc As Inventor.AssemblyDocument
Set oAssemblyDoc = ThisApplication.ActiveDocument
Dim oDoc As Document

For Each oDoc In oAssemblyDoc.AllReferencedDocuments
    If oDoc.DocumentType = kPartDocumentObject Then
        If oAssemblyDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count > 0 Then
            strLocation = ThisApplication.FileOptions.TemplatesPath
            Set oDrgDoc = ThisApplication.Documents.Add(kDrawingDocumentObject, strLocation & "MasterBeam.idw")
            Dim oSheet As Sheet
            Set oSheet = oDrgDoc.Sheets.Item(1)

            Dim oPoint1 As Point2d
            Set oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(15#, 25#)

            Dim oPoint2 As Point2d
            Set oPoint2 = ThisApplication.TransientGeometry.CreatePoint2d(15#, 15#)

            Dim oView1 As DrawingView
            Set oView1 = oSheet.DrawingViews.AddBaseView(oDoc, oPoint1, 0.1, kBottomViewOrientation, kHiddenLineDrawingViewStyle)
        End If
    End If
Next
End Sub

 


 

Message 12 of 23
JhoelForshav
in reply to: sam

Hi @sam 

If I understand correctly you want the code to generate a drawing for a part file if thats whats open, and for every part in an assembly if an assembly is open?

Then something like this should do the trick 🙂

I moved the creation of the drawing to its own sub. The main sub now checks wether an assembly or part is the active document. If it's an assembly it'll call the CreateDrawing-sub for each part within. If it's a part it'll only call the sub once for that specific part 🙂

 

Sub btnGenerateDrawing_Click()
Dim oActiveDoc As Document
Set oActiveDoc = ThisApplication.ActiveDocument
If oActiveDoc.DocumentType = kAssemblyDocumentObject Then

Dim oDoc As Document

For Each oDoc In oActiveDoc.AllReferencedDocuments
    If oDoc.DocumentType = kPartDocumentObject Then
        If oActiveDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count > 0 Then
           Call CreateDrawing(oDoc)
        End If
    End If
Next
ElseIf oActiveDoc.DocumentType = kPartDocumentObject Then
Call CreateDrawing(oActiveDoc)
End If
End Sub

Sub CreateDrawing(oDoc As PartDocument)
strLocation = ThisApplication.FileOptions.TemplatesPath
Set oDrgDoc = ThisApplication.Documents.Add(kDrawingDocumentObject, strLocation & "MasterBeam.idw")
Dim oSheet As Sheet
Set oSheet = oDrgDoc.Sheets.Item(1)

Dim oPoint1 As Point2d
Set oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(15#, 25#)

Dim oPoint2 As Point2d
Set oPoint2 = ThisApplication.TransientGeometry.CreatePoint2d(15#, 15#)

Dim oView1 As DrawingView
Set oView1 = oSheet.DrawingViews.AddBaseView(oDoc, oPoint1, 0.1, kBottomViewOrientation, kHiddenLineDrawingViewStyle)
End Sub

 

 

Message 13 of 23
_dscholtes_
in reply to: JhoelForshav

@JhoelForshav I have a question regarding your code.

For Each oDoc In oActiveDoc.AllReferencedDocuments
    If oDoc.DocumentType = kPartDocumentObject Then
       --> If oActiveDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count > 0 Then
           Call CreateDrawing(oDoc)
        End If
    End If
Next

 

Why the addition check? (indicated with -->) Now it seems like it's possible that oDoc is a member of AllReferencedDocuments, but there's no occurrence in the assembly based on oDoc. Could you explain this a bit, please?

Message 14 of 23
JhoelForshav
in reply to: _dscholtes_

Hi @_dscholtes_ 

AllReferencedDocuments literally means ALL referenced documents. So for example if I have an occurrence of a part in my assembly and that part has some derived parameters. The document from which these parameters are derived will be included in AllReferencedDoucments aswell. In this case we only want documents that's represented as occurrences in the assembly 🙂

Message 15 of 23
_dscholtes_
in reply to: JhoelForshav

@JhoelForshav Interesting. The few pieces of code I found and used as reference do not mention the possibility of your example (that a document is in the 'AllRefDoc' list because of derived parameters). They're al written with the intention of going through documents for their contents, mostly being parameter sets.

Did you learn this by accident, by lecture or is there an article I can read to get more info about this?

 

I think I will have to check my existing macros on the use of AllReferenceDocuments and see if I need to add this check as well.

 

@sam Sorry for hijacking the thread.

Message 16 of 23
sam
Advocate
in reply to: _dscholtes_

@_dscholtes_  No worries, please carry on - I am learning something new.

Message 17 of 23
JhoelForshav
in reply to: _dscholtes_

@_dscholtes_ 

There's a difference between an occurrence and a document. When we talk about referenced documents its documents referenced by the document in question, not its occurrences. Although in order to have an occurrence of a document there has to be a reference to that document. But in order to have derived components from a document you also must have a reference to that document. AllReferencedDocuments looks att the documents referenced by the assembly, then att the documents referenced by those documents and so on. See attached screencast as a demonstration of the difference between checking if the assembly contains any occurrences of the document and not checking 🙂

 
Message 18 of 23
JhoelForshav
in reply to: sam

@_dscholtes_ 

I tried uploading a screencast to demonstrate this but for some reason the entire post just disappeared, so I'll try again.

There's a difference between a document and an occurrence. In order for an assembly to contain an occurrence of a part/assembly it has to have a reference to that part/assemblys document. In order for a part to contain derived geometry from another part it has to have a reference to that parts document aswell. When we use AllReferencedDocuments in an assembly we look att all the documents that assembly has a reference to, and also all the documents that referenced document has a reference to and so on. Hopefully the screencast will work in this post 🙂

 

 

Message 19 of 23
_dscholtes_
in reply to: JhoelForshav

@JhoelForshav Thanks for the screencast. It shows the difference pretty clear.

 

I based my usage of AllReferencedDocuments on this article, but there are similar examples around. They're all about accessing assembly components, meaning all the documents in these examples are used by an occurrence. No article stated that there could be documents in AllReferencedDocuments which are not used by occurrences. And so far, I haven't encountered an issue with my own macros.

To expand my knowledge, are there other ways for a document to show up in AllReferencedDocuments without it having a corresponding occurrence, apart from deriving?

 

@sam Same here.

Message 20 of 23
sam
Advocate
in reply to: JhoelForshav

@JhoelForshav  - It is perfect, thank you. 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report