Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

[iLogic] How to open drawings of the selected components in assembly ?

王承之pmhker
Advisor

[iLogic] How to open drawings of the selected components in assembly ?

王承之pmhker
Advisor
Advisor

I want to run a ilogic rule and select components in assembly browser, then open drawings of them. 

Here is my existing code,However, it can only open drawings in the same folder as the part。

how to edit the code to open drawings in all folders in a workspace。

thanks

 

Imports File = System.IO.File
Imports Path = System.IO.Path
Sub main
Dim oAsmDoc = ThisDoc.Document
If oAsmDoc.DocumentType <> kAssemblyDocumentObject Then
 MsgBox("Must running in an assembly!", , "Pmhker")
 Return
End If
Dim oDoc As AssemblyDocument = ThisDoc.Document
oCompDef = oDoc.ComponentDefinition
Dim oSet As HighlightSet = oDoc.CreateHighlightSet
Dim comps As ObjectCollection
Dim comp As Object
comps = ThisApplication.TransientObjects.CreateObjectCollection
While True
 comp = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter,"Select components,Press_Esc_to finish select")
 If IsNothing(comp) Then Exit While
 comps.Add(comp)
 oSet.AddItem(comp)
End While
For Each comp In comps
 ofile = comp.Definition.document
 ofilename = ofile.fullfilename
 fullname = Path.ChangeExtension(ofilename, ".idw")
 If File.Exists(Path.ChangeExtension(fullname, ".idw")) Then
  ThisApplication.Documents.Open(fullname, True)
  'ThisDoc.Launch(fullname & IDWType)
Else 
	MsgBox("Drawing of" & ofilename & "can't found",,"Pmhker")
 End If
Next
End Sub


If my post answers your question, please click the "Accept as Solution" button. This helps everyone find answers more quickly!
如果我的回帖解决了您的问题,请点击 "接受为解决方案" 按钮. 这可以帮助其他人更快的找到解决方案!


王 承之
Autodesk AGN [Inventor 俱乐部] Leader
Inventor Club | Bilibili


AGN L    EESignature

0 Likes
Reply
826 Views
8 Replies
Replies (8)

j.brodersen
Enthusiast
Enthusiast

Hi @王承之pmhker 

 

the problem is

 

ofilename = ofile.fullfilename

it has the path to the folder where your parts are saved,  so this line

ThisApplication.Documents.Open(fullname, True)

will lock for the idw in the same folder.

 

Do you save your idw files always in the same location like eg 

 

ThisDoc.WorkspacePath & "\MyDrawings\ 

 if so following could do the job

 

Imports File = System.IO.File
Imports Path = System.IO.Path
Sub Main
Dim oAsmDoc = ThisDoc.Document
If oAsmDoc.DocumentType <> kAssemblyDocumentObject Then
 MsgBox("Must running in an assembly!", , "Pmhker")
 Return
End If
drwPath = ThisDoc.WorkspacePath & "\MyDrawings\" 'Change MyDrawings to the name of the folder where your drawings are saved and ThisDoc.WorkspacePath if the drawings aren't saved in the project location

Dim oDoc As AssemblyDocument = ThisDoc.Document
oCompDef = oDoc.ComponentDefinition
Dim oSet As HighlightSet = oDoc.CreateHighlightSet
Dim comps As ObjectCollection
Dim comp As Object
comps = ThisApplication.TransientObjects.CreateObjectCollection
While True
 comp = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter,"Select components,Press_Esc_to finish select")
 If IsNothing(comp) Then Exit While
 comps.Add(comp)
 oSet.AddItem(comp)
End While
For Each comp In comps
	
 ofile = comp.Definition.document
 ofilename = ofile.fullfilename

 'to get only the filename without path 
 justFilename = Right(ofilename, Len(ofilename)-InStrRev(ofilename, "\", -1))

 fullname = Path.ChangeExtension(justFilename, ".idw")

 If File.Exists(drwPath & fullname) Then
  ThisApplication.Documents.Open(drwPath & fullname, True)
  'ThisDoc.Launch(fullname & IDWType)

Else 
	MsgBox("Drawing of" & ofilename & "can't found",,"Pmhker")
 End If
Next

End Sub

王承之pmhker
Advisor
Advisor

thanks for your reply。

but the name of folder that drawings saved is not always the same. Maybe some are in “WorkspacePath\DrawingA\"  and some are in “WorkspacePath\DrawingB\"


If my post answers your question, please click the "Accept as Solution" button. This helps everyone find answers more quickly!
如果我的回帖解决了您的问题,请点击 "接受为解决方案" 按钮. 这可以帮助其他人更快的找到解决方案!


王 承之
Autodesk AGN [Inventor 俱乐部] Leader
Inventor Club | Bilibili


AGN L    EESignature

0 Likes

nedeljko.sovljanski
Advocate
Advocate

Hi @王承之pmhker 

Use AssemblyDocument.ReferencingDocuments to list Inventor document where your file is used. Filter only drawing type, and be aware your assembly can be used in more than one drawing.

0 Likes

j.brodersen
Enthusiast
Enthusiast

@王承之pmhker 

 

I butchered your code a little bit and following worked for me

 

 

 

Imports File = System.IO.File
Imports Path = System.IO.Path
Sub Main
Dim oAsmDoc = ThisDoc.Document
If oAsmDoc.DocumentType <> kAssemblyDocumentObject Then
 MsgBox("Must running in an assembly!", , "Pmhker")
 Return
End If
drwPath = ThisDoc.WorkspacePath & "\" 'Change MyDrawings to the name of the folder where your drawings are saved and ThisDoc.WorkspacePath if the drawings aren't saved in the project location


Dim oDoc As AssemblyDocument = ThisDoc.Document
oCompDef = oDoc.ComponentDefinition
Dim oSet As HighlightSet = oDoc.CreateHighlightSet
Dim comps As ObjectCollection
Dim comp As Object
comps = ThisApplication.TransientObjects.CreateObjectCollection
While True
 comp = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter,"Select components,Press_Esc_to finish select")
 If IsNothing(comp) Then Exit While
 comps.Add(comp)
 oSet.AddItem(comp)
End While

For Each comp In comps
	
 ofile = comp.Definition.document
 ofilename = ofile.fullfilename

 'to get only the filename without path 
 justFilename = Right(ofilename, Len(ofilename)-InStrRev(ofilename, "\", -1))

 fullname = Path.ChangeExtension(justFilename, ".idw")
 
 
Dim drwfolder As String 
Dim dirs As String()
Dim dir As String

Try
 dirs = System.IO.Directory.GetDirectories(drwPath)

     For Each dir In dirs
		If  System.IO.File.Exists(dir & "\" & fullname) Then

        drwfolder = dir

       End If
Next
Catch
	
    drwfolder = "" 

End Try

If drwfolder = "" Then
	
MsgBox("Drawing of" & ofilename & "can't found", , "Pmhker")

Return

Else

  ThisApplication.Documents.Open(drwfolder & "\"  & fullname, True)
	
 End If

Next

End Sub

 

 

  I'm sure that a few lines can be simplified like using dir.contains(fullname) instead of system.IO.File.Exists but I'm unfortunately not experienced enough to get it working  

 

Maybe you can figure it out if you check following link

 

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/opening-drawing-directly-from-model-...

 

there I found the pieces i needed  for the "loop through all sub folders" part

0 Likes

王承之pmhker
Advisor
Advisor

When I run this rule, an error is displayed in line 63


If my post answers your question, please click the "Accept as Solution" button. This helps everyone find answers more quickly!
如果我的回帖解决了您的问题,请点击 "接受为解决方案" 按钮. 这可以帮助其他人更快的找到解决方案!


王 承之
Autodesk AGN [Inventor 俱乐部] Leader
Inventor Club | Bilibili


AGN L    EESignature

0 Likes

j.brodersen
Enthusiast
Enthusiast

@王承之pmhker 

 

can you share the error message and did you change something because line 63 is empty with me?

0 Likes

王承之pmhker
Advisor
Advisor

And it can't open darwing  in the same folder as the part

 

微信截图_20220721172333.png微信截图_20220721172419.png


If my post answers your question, please click the "Accept as Solution" button. This helps everyone find answers more quickly!
如果我的回帖解决了您的问题,请点击 "接受为解决方案" 按钮. 这可以帮助其他人更快的找到解决方案!


王 承之
Autodesk AGN [Inventor 俱乐部] Leader
Inventor Club | Bilibili


AGN L    EESignature

0 Likes

j.brodersen
Enthusiast
Enthusiast

@王承之pmhker 

 

ok sorry but my chines is not existing so i can only assume which kind of error you got 😅

 

Just tried to move the .idw to differed folder locations (including the one where the part file is located) in the project path but i can't get the code to "crash" /give me an error, it opens the idw each time of the part I've selected. Only exception was when the idw was directly in the WorkspacePath folder, there i got the msgbox you added when no idw was found for the prt

 

Could you try to copy the code and insert is again? From my side it looks like that one line is missing somewhere because I've a total of 70 lines and with you it's ending at 69.

 

It would as well be interesting to know what 

drwfolder & "\"  & fullname

is on your end because that's the only thing I think could cause the error. If everything works fine drwfolder should show you where the idw is located (In your case the folder where the parts are)  and fullname the name of the idw file it selfe. 

0 Likes