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
Solved! Go to Solution.
Solved by JhoelForshav. Go to Solution.
Solved by JhoelForshav. Go to Solution.
Solved by JhoelForshav. Go to Solution.
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)
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
@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
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
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
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
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
@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.
I will try to do as much as I can but I might bug you if stuck somewhere.
regards,
sam
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.
@Anonymous Please read this thread to understand the different and how to use the code interchangeably.
regards,
sam
@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
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
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
@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?
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 🙂
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
@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.
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 🙂
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
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 🙂
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
@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.
Can't find what you're looking for? Ask the community or share your knowledge.