Open All Drawings Related To Top Level, Parts & Sub-Assemblies Given Assembly File

Open All Drawings Related To Top Level, Parts & Sub-Assemblies Given Assembly File

gareth.butlerS9R7M
Advocate Advocate
766 Views
9 Replies
Message 1 of 10

Open All Drawings Related To Top Level, Parts & Sub-Assemblies Given Assembly File

gareth.butlerS9R7M
Advocate
Advocate

Hi All,

 

I'm looking for some help with an iLogic code I have as shown below.

 

The code below opens all related drawing files from the within the assembly file but only if the files have the exact same name and file path.

 

Sub Main()

'Check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
Exit Sub
End If

'Define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
Dim oAsmName As String
oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)

'Component drawings
'Look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments
Dim oRefDoc As Document

'Work with the drawing files for the referenced models
'This expects that the model has a drawing of the same path and name
For Each oRefDoc In oRefDocs
dwgPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "dwg"

'Check to see that the model has a drawing of the same path and name
If(System.IO.File.Exists(dwgPathName)) Then
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.Documents.Open(dwgPathName, True)
End If
Next

End Sub

 

Preferred folder search path:

R:\1. Projects\1. Tool Connection\

 

Is there a way to expand this code and make iLogic search the preferred path, say by using some sort of wildcard etc?

 

I'm looking for a similar function to Open Drawing as shown in the attached image but which opens all associated part and sub-assembly drawings of a given assembly, not just the drawing for the top level assembly.

 

Any help greatly appreciated! 😀

 

Many thanks,

Gareth

0 Likes
767 Views
9 Replies
Replies (9)
Message 2 of 10

dalton98
Collaborator
Collaborator

This will probably need some optimization for your specific task. Are you wanting to track which parts do/dont have drawings? Are you wanting to migrate drawings to that specific folder?

 

Add the top line at the beginning of your code and change the value of 'dwgPathName'

 

Dim pathName As String = "R:\1. Projects\1. Tool Connection\"
''
''
''
dwgPathName = pathName & IO.Path.GetFileNameWithoutExtension(oRefDoc.FullDocumentName) & ".dwg" 
Message 3 of 10

WCrihfield
Mentor
Mentor

Hi @gareth.butlerS9R7M.  Here is what I came up with for you:

Sub Main()

	'Check that the active document is an assembly file
	If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
		MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
		Exit Sub
	End If
	
	'Define the active document as an assembly file
	Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oAsmName As String = System.IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName)
	Dim oDrawingsPath As String = "R:\1. Projects\1. Tool Connection\"
	'Component drawings
	'Look at the files referenced by the assembly
	Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
	Dim oRefDoc As Document
	
	'Work with the drawing files for the referenced models
	'This expects that the model has a drawing of the same path and name
	For Each oRefDoc In oRefDocs
		Dim oFileName As String = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
		Dim dwgPathName As String = oDrawingsPath & oFileName & ".dwg"
		'Check to see that the model has a drawing of the same path and name
		If System.IO.File.Exists(dwgPathName) Then
			Dim oDrawDoc As DrawingDocument = ThisApplication.Documents.Open(dwgPathName, True)
		End If
	Next
End Sub

Edit:  Just saw the other response after posting, so this is pretty similar.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 10

gareth.butlerS9R7M
Advocate
Advocate

Hi Dalton,

 

Cheers for the quick response!

 

I am looking for the iLogic to open all drawings relating to an open assembly.

 

e.g.

 

I have assembly 0001.iam open. This resides in Folder A

This references parts and assemblies which also reside in Folder A:

0002.ipt

0003.ipt

0004.ipt

0005.iam

etc.

 

The drawing for the assembly is 0001.dwg. This resides in Folder B

The drawings for the parts also reside in Folder B:

0002.dwg

0003.dwg

0004.dwg

0005.dwg

etc.

 

When I have 0001.iam open, I want to run iLogic code from within which opens the drawings 0001.dwg, 0002.dwg, 0003.dwg, 0004.dwg, 0005.dwg etc.

 

If the code could drill down further, so that the drawings for the parts and assemblies within 0005.iam are pulled through, even better still! Sounds complex though!

 

Thanks,

Gareth

0 Likes
Message 5 of 10

gareth.butlerS9R7M
Advocate
Advocate

Hi WCrihfield,

 

Many thanks for your quick response!

 

I have tried out the code you very kindly provided and it works but only if the assembly, part and drawing files have the exact same name and path.

 

Please see reply to Dalton for more detail on what I am trying to achieve.

 

Cheers,

Gareth

0 Likes
Message 6 of 10

WCrihfield
Mentor
Mentor

OK.  So you need a 'recursive' sub routine that will process every level of the main assembly, and every level of every sub-assembly, opening every drawing for everything possible, right?  Well, if the model files and their drawing files are not located in the same folder, how can we know the exact path of the folder where we can find the drawings?  Is there always a sub-folder just for drawing files, just under every folder that holds model files (and if so, what is that sub-folder's name?)?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 10

gareth.butlerS9R7M
Advocate
Advocate

Hi WCrihfield,

 

"OK.  So you need a 'recursive' sub routine that will process every level of the main assembly, and every level of every sub-assembly, opening every drawing for everything possible, right?"

 

Correct, although I see your point now in terms of knowing the folder reference paths!

 

I think this might be asking a bit too much as it is multi-level / multi-folder.

 

You are going to love me for this, but hopefully the attached images will kind of explain the folder structuring.

 

Drawing Folder - Project TC00001.jpg3D Models Folder - Project TC00001.jpgDrawing Folder - Project TC00003.jpg3D Models Folder - Project TC00003.jpgDrawing Folder - Project TC00003 - Revision Level Sub-Folders.jpg3D Models Folder - Project TC00003 - Revision Level Sub-Folders.jpg

 

Many thanks,

Gareth

0 Likes
Message 8 of 10

dalton98
Collaborator
Collaborator

@gareth.butlerS9R7M are the drawings in the same project folder as the assembly? If so you could just use 'ThisDoc.Launch(part.idw)'. Heres an example

Dim oAss As AssemblyDocument = ThisApplication.ActiveDocument
Dim oRefDoc As Document

For Each oRefDoc In oAss.AllReferencedDocuments
	oDrawingName = IO.Path.GetFileNameWithoutExtension(oRefDoc.FullDocumentName) & ".idw"
	Try
		ThisDoc.Launch(oDrawingName)
	Catch
	End Try

Next
Message 9 of 10

WCrihfield
Mentor
Mentor

😦 That is one...special file structure there.  That will surely make things interesting.  Out of curiosity, what do you plan to do with all of those drawing documents, once you have them all visibly open in your Inventor application?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 10

gareth.butlerS9R7M
Advocate
Advocate

Hi WCrihfield,

 

It is indeed, it is based on a sister company file structure that was in place before I started, just throw them under the bus. 😂

 

I'm starting to see that there may be a better way of handling the files and folders.

 

I think they maybe should all be put under one massive folder and then everything should work! 😂

 

The main intention is to open to view. Secondary functions would be to print out and / or export to PDF / DXF.

 

Many thanks,

Gareth

0 Likes