iLogic: Get Document object from full file path

iLogic: Get Document object from full file path

DRoam
Mentor Mentor
7,240 Views
11 Replies
Message 1 of 12

iLogic: Get Document object from full file path

DRoam
Mentor
Mentor

I have an iLogic rule which gets the full file path for the selected Part in a drawing view and places it in a Shared Variable so another rule can access it.

 

I have a second rule which needs to place a View of that Part on the current sheet. This has to be a separate rule, because I want to place the view on a different sheet (or even a different drawing). But in order to do this, the second rule needs to have access to the Document object for the Part.

 

So my question is, how can I make Inventor get the document object for a specific Part file, given the Part file's full file path?

 

I need something like:

 

'Mock code:
Dim oPartDoc As Document
oPartDoc = Document("C:\Documents\MyPartFile.ipt")

 

To borrow @Curtis_Waguespack's analogy... I may be trying to bend the spoon when there is no spoon Smiley Wink If I can place a view of my Part using just its filepath without needing the Document object itself, that will work, too.

 

0 Likes
Accepted solutions (1)
7,241 Views
11 Replies
Replies (11)
Message 2 of 12

Curtis_Waguespack
Consultant
Consultant

Hi DRoam,

 

I might be wrong, but I think you have to load the file with either the Open or OpenWithOptions methods.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

'imagine the spoon
sPathName = "C:\Temp\Spoon.ipt"

'make the spoon "real"
'True opens visible, false opens invisible
oDoc = ThisApplication.Documents.Open(sPathName,True) 

'see the spoon
	MessageBox.Show("Bend spoon as needed.", "iLogic")

'make the spoon vanish
oDoc.Close

 

 

'imagine the spoon
sPathName = "C:\Temp\Spoon.iam"

' Create a new NameValueMap object
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap

' Set the options to use when opening the document.
Call oOptions.Add("DesignViewRepresentation", "View2")
Call oOptions.Add("LevelOfDetailRepresentation", "Master")
Call oOptions.Add("PositionalRepresentation", "Position1")

'make the spoon "real"
oDoc = ThisApplication.Documents.OpenWithOptions(sPathName, oOptions, True)

'see the spoon
	MessageBox.Show("Bend spoon as needed.", "iLogic")

'make the spoon vanish
oDoc.Close

EESignature

Message 3 of 12

DRoam
Mentor
Mentor

Thank you, Curtis, this works. Only problem is, it closes the document even if I already had it open when I ran the rule, which isn't ideal.

 

Is there any way I can have the rule check if the document is already open before the rule "opens" it? Then I can only have the rule close it if it wasn't already open.

 

 

0 Likes
Message 4 of 12

DRoam
Mentor
Mentor

Funnily enough, in my search to find a way to check if the Part document is already open, I stumbled across a way to access the Document object through this very check rather than having to use the Open command. Reason being, the code I tried doesn't iterate through the "physically" open documents that I can see in my opened document tabs at the bottom of the screen; rather, it iterates through all of the documents Inventor has loaded into the session. And since my Part is in a Drawing view that I just used to get it from, it's always guaranteed to be loaded when I run my second rule (unless I opened the drawing with "Skip All" turned on).

 

So, that said, I'm able to access my Document object with this code:

 

 

Dim oDoc As Document
Dim oPartDoc As PartDocument
For Each oDoc In ThisApplication.Documents
	If oDoc.FullFileName = SharedVariable("myPartPath") Then
		oPartDoc = oDoc
		Exit For
	End If
Next

 

 

In the above code, the "myPartPath" shared variable is the full file path for my Part which I got from running my first rule.

 

I would prefer the "open" method with a way to check if the file is physically opened, because that doesn't rely on the Part being loaded into memory, which might be nice if perhaps I opened the drawing with "Skip All" turned on. So if anyone knows of a way to check if a Part file is physically opened in Inventor, that would be great. (Of course, a way to directly access the document object given the full file path would be even better).

 

But in the absence of either of those, the above code is what I'll use.

 

 

Message 5 of 12

Curtis_Waguespack
Consultant
Consultant

Hi DRoam,

 

I'm short on time, but I would try using the ThisApplication.Documents.VisibleDocuments collection.

 

Put that collection in array list, and then check the array list for the document, and only if it's not found close it.

 

Or something like that.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

Message 6 of 12

MechMachineMan
Advisor
Advisor
Accepted solution

So if you are grabbing the full file path all of the time for this, is it safe to say you are not using Level of Detail ever. If this is the case, something like this should work for you.

 

On Error Resume next
    oDoc = ThisApplication.Documents.ItemByName(strFilePath)
    If Err.Number <> 0 Then
        oDoc = ThisApplication.Documents.Open(strFilePath, False)
        _DocOpened = True
    End if
On Error GoTo 0


'Do stuff

If _DocOpened = True
    oDoc.Close
End if

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 7 of 12

DRoam
Mentor
Mentor

Thank you, @Curtis_Waguespack, the VisibleDocuments collection works perfectly for determining if the document is physically opened.

 

But @MechMachineMan, that code works perfectly and does just what I need, thank you!

 

The only thing I changed is to make it more "packaged" by simply closing the document immediately after it was opened, since I don't really need it open anymore now that I've grabbed the document object. I also changed it to a Try/Catch to make it a little more compact. You may know of reason that "On Error" is better (I'm not very knowledgeable about error handling); if so, please let me know.

 

Either way, in the end it looks like this:

 

Try
    oPartDoc = ThisApplication.Documents.ItemByName(myPartPath)
Catch
    oPartDoc = ThisApplication.Documents.Open(myPartPath, False)
    oPartDoc.Close
End Try

 

This is nice and compact and does just what I need. Thanks for providing the code to get me there.

 

And thanks again, Curtis, for your help. I'm sure I'll be able to use those useful snippets in the future, too.

Message 8 of 12

MechMachineMan
Advisor
Advisor
What's the purpose of opening and closing the document if you are just
discarding it right away without performing any operations?

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 9 of 12

DRoam
Mentor
Mentor

Ok, so it turns out if I opened the drawing with "Skip All" turned on, I can't get the selected Part's file path in the first place because it's not loaded in. So it's safe to say I'll never be using this with Skip All turned on.

 

So the only time opening the document would be necessary would be if I captured my Part's file path, then closed my drawing (and any other instances of the Part), and then tried to place a view. Which I'll probably never do.

 

But I did test it, and if I do that, the "Close" command does indeed have to be after I've "done stuff" with my Part's Document object; apparently Inventor needs the document loaded in order to use the Document object, which makes sense (I should have thought of that before Smiley Embarassed).

 

 

So, @MechMachineMan's code is indeed the simplest way to be certain of having the Document object when I need it. My apologies for confusing things.

 

Unless there's a good reason not to, I will probably go with the Try/Catch statement just for simplicity's sake. But other than that, the code you provided is perfect. Thanks again!

0 Likes
Message 10 of 12

MechMachineMan
Advisor
Advisor

Try/Catch is fine.

 

I prefer On Error because it is easier to convert to vba should the need arise, and lately I do most of my coding in VBA. (Using Excel as a user interface).

 

Also, you CAN access the model file name if you open the drawing as skip all.

 

oDwgDoc.ReferencedFileDescriptors

OR

Dim oView As DrawingView
oView = ThisApplication.ActiveDocument.ActiveSheet.DrawingViews(1)
MsgBox(oView.ReferencedDocumentDescriptor.FullDocumentName)
MsgBox(oView.ReferencedDocumentDescriptor.ReferencedFileDescriptor.FullFileName)

OR.

     Other Possible Ways. (ie; through accessing BOM Doc File)


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 11 of 12

DRoam
Mentor
Mentor

That makes sense on the VBA conversion.

 

Regarding getting the file path with Skip All enabled... is that still possible if I'm trying to get the path for a Part selected in a drawing View of an Assembly, and not a drawing View of the Part itself?

 

Here is the code I'm using to do this:

 

Dim oRuleName As String = "Capture Part from View"

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisDoc.Document

oSelectSet = oDrawDoc.SelectSet

'Get the selected Part document
If oSelectSet.Count > 0 Then
	oSelectedObject = oSelectSet(1)
	Dim oObject As Object
	Dim oView As DrawingView
	If TypeOf(oSelectedObject) Is GenericObject Then
		Dim oGenericObject As GenericObject = oSelectedObject
		oDrawDoc.ProcessViewSelection(oGenericObject, oView, oObject)
		If Not oObject Is Nothing Then
			'Try to see if it's a component occurrence
			oOccurrence = TryCast(oObject, ComponentOccurrence)
			If Not oOccurrence Is Nothing Then
				oPartDoc = oOccurrence.Definition.Document
			End If
		End If
	End If
Else
	MessageBox.Show("You must select a Part first.", oRuleName,MessageBoxButtons.OK,MessageBoxIcon.Error)
End If

SharedVariable("myPartPath") = oPartDoc.FullFileName
MessageBox.Show("Part """ & SharedVariable("myPartPath")& """ has been captured for View placement.",oRuleName)

Can this be modified to get the Part's file path even if Skip All is on?

 

I'm guessing not since, unlike a Part which is the direct subject of a View, a Part in an Assembly view isn't directly referenced by the Drawing.

 

0 Likes
Message 12 of 12

MechMachineMan
Advisor
Advisor

You guess correctly. 

 

When you load as skip all, everything is converted to dumb lines and no model information is loaded except for the one to the original top level assembly. But even so, it is just loaded as a string, and not any useful information.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes