Can I set a document object to a file with a file path?

Can I set a document object to a file with a file path?

dustinbagley
Advocate Advocate
467 Views
8 Replies
Message 1 of 9

Can I set a document object to a file with a file path?

dustinbagley
Advocate
Advocate

Can I set a document object to a file with a file path? 

Dim Path As String = "C:\Vault\Drawings\"
Dim DrwStr As String = "0002289721.idw"
Dim oDrawDoc As DrawingDocument = System.IO.File(Path & DrwStr)

 

0 Likes
Accepted solutions (1)
468 Views
8 Replies
Replies (8)
Message 2 of 9

WCrihfield
Mentor
Mentor

Hi @dustinbagley.  This is an odd question.  Files and documents are two different things.  A file is what gets saved to a hard drive, with a file address.  A document is basically just a reference to the organized data being held in Inventor's session memory, which may have been at least partially read from a file, or created within the software, and has not been saved as a File to the hard drive yet.  A Document object has a ReadOnly property named File, which will return Inventor's version of a File object.  That Inventor File object then has a Read/Write property named FullFileName, where you can only 'set' its value when that File has not been saved yet.  Once the Document/File has been saved, that either creates or updates the File on the hard drive, but leaves the Document open in Inventor's session memory, until you Close it.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 9

WCrihfield
Mentor
Mentor

Or maybe you are simply asking about how to open File on disk, and get a reference to the Document that gets loaded into Inventor's session memory when that happens.  Here is an alternate version of your code where an example of that is used:

Dim Path As String = "C:\Vault\Drawings\"
Dim DrwStr As String = "0002289721.idw"
Dim oDrawDoc As DrawingDocument = ThisApplication.Documents.Open(Path & DrwStr, True)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 9

dustinbagley
Advocate
Advocate

@WCrihfield to be more specific, what I'm trying to do, is access custom iProperties of a drawing. I have access to the assembly that is represented by the drawing and I can get the filename of the drawing by virtue of the fact that the drawings always share the same name as the part or assembly that they represent. I can set a drawing to an object when I use the open function:

Dim oDrawDoc As DrawingDocument = ThisApplication.Documents.Open(c:\vault\drawings\drawing.idw)

 But I'd like to be able to get a drawing set to an object under the same circumstances without having to open it. Thank you for your help.

0 Likes
Message 5 of 9

WCrihfield
Mentor
Mentor
Accepted solution

OK.  So you currently have an assembly open on your screen, and you know that there is a drawing for that assembly, and you want to access the iProperties of that drawing from that assembly, without opening that drawing, right?  Well, from an iLogic rule, you can only access the iProperties of an open drawing.  The drawing does not necessarily need to be 'visibly' open, but it does need to be open.  You can specify False as the last input when using that Documents.Open method, and the document will be opened invisibly (in the background, no document tab shown).

 

Or, if the drawing is already open, but is simply not the 'active' document, you can get a reference to it through the ThisApplication.Documents collection using the Documents.ItemByName() property, where it will let you specify the FullDocumentName of the drawing.

Either way, once you have the Document (or DrawingDocument) object, you can use its Document.PropertySets, then PropertySets.Item property to access a specific PropertySet, then use the PropertySet.Item property to get a specific Property object, then Property.Value to access its value.  Unfortunately, you would not be able to use the iLogic snippet iProperties.Value("SetName", "PropertyName") route, because even if you specified the first (of 3) inputs in an attempt to specify a document, it can only access a document that is within the scope of the active documents 'AllReferencedDocuments' collection...and the drawing will not be in that collection.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 9

dustinbagley
Advocate
Advocate

@WCrihfield Thanks for explaining that to me. I'll use the non-visible open to read iproperties. thanks

0 Likes
Message 7 of 9

WCrihfield
Mentor
Mentor

Here is another example iLogic code starter, where I have implemented both of those options mentioned above.  It first captures the current document do a generic Document type variable using the ThisDoc.Document phrase.  Then if it is already a drawing, it will set that document reference as the value of a DrawingDocument type variable, so we have access to all the methods & properties of the drawing.  If the active document is not a drawing, it then tries to get the full file name of the drawing.  I am using two lines of code there that assume that the drawing is saved in the same path as the current document, and has the same file name as the current document, but has the different file extension.  If that is not the case, that part can be changed as needed.  I did notice that you seem to be using Vault, so you may simply need to substitute in the vault path, as in your first couple posts.  Once it has a full file name for the drawing, it first checks to see if it is already open in the background, and if so, gets that document reference to the DrawingDocument type variable.  If not found there, it then tries to open the drawing in a not visible way.  If either of those worked to set the value of the DrawingDocument type variable, it then moves on to access the custom iProperties of that drawing.  I am showing the Inventor API route of doing that part.  I first get the PropertySet for the 'custom' iProperties.  This can be done in a few different ways (by name, internal name, or Index number).  Then I create a variable for an Inventor Property type object, with no value set to it yet.  Then I use a Try...Catch block of code to avoid getting an error when I try to access a custom iProperty that may not already exist (if checking or changing a value).  If getting the existing property fails, the Catch part runs, and will either notify the user that the property could not be found, or creates the custom iProperty.

Dim oDoc As Document = ThisDoc.Document
Dim oDrawDoc As Document = Nothing
If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
	oDrawDoc = oDoc
Else
	'these next two lines assume the drawing has same path & file name as model, except extension
	Dim sDrawingFile As String = ThisDoc.PathAndFileName(False) & ".idw"
	'Dim sDrawingFile As String = System.IO.Path.ChangeExtension(oDoc.FullFileName, ".idw")
	If System.IO.File.Exists(sDrawingFile) = False Then Exit Sub 'exit rule, the file does not exist
	For Each oOpenDoc As Document In ThisApplication.Documents
		If oOpenDoc.FullFileName = sDrawingFile Then
			oDrawDoc = oOpenDoc
		End If
	Next
	If oDrawDoc Is Nothing Then
		oDrawDoc = ThisApplication.Documents.Open(sDrawingFile, False) 'False = not visible
	End If
End If
If oDrawDoc Is Nothing Then Exit Sub 'exit rule because no drawing could be found
Dim oCustomPropSet As Inventor.PropertySet = oDrawDoc.PropertySets.Item("Inventor User Defined Properties")
'or oDrawDoc.PropertySets.Item(4) 'the 'custom' set is always the fourth set
Dim oCustomProp As Inventor.Property = Nothing
'if you try to get a custom property by name, and it is not found, it will throw an error
'use Try...Catch...End Try block to avoid the error, and let rule keep running, if needed
Try 
	oCustomProp = oCustomPropSet.Item("Custom Property Name")
Catch
	'it was not found, so what to do now (create it, or let user know it was not found)
	'MsgBox("Custom Property Was Not Found", vbCritical, "Property Not Found")
	oCustomProp = oCustomPropSet.Add("Custom Property Value", "Custom Property Name")
End Try

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 9

WCrihfield
Mentor
Mentor

Sorry, I posted that last response before noticing your previous response.  I was typing a long time.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 9

dustinbagley
Advocate
Advocate

@WCrihfield No worries, I'm glad you made that other post because it will simplify my code a bit more with the switch extension function. I was not aware of that and as you can see below, I was working a lot harder than I was needing to. Thank for your help.

 

'[Gets drawing file name from assembly/part filename
FullFileName = oAssyDoc.FullFileName
'strips path
FileName = Right(FullFileName, Len(FullFileName) -InStrRev(FullFileName, "\"))
'strips extension
Name = Left(FileName, Len(FileName) -4)
'adds drawing path and extension
DrawingFileName = DrawingPath & Name & ".idw"
']

 

0 Likes