Skip Read-only or not Modifiable file.

Skip Read-only or not Modifiable file.

DewayneH
Advocate Advocate
1,572 Views
7 Replies
Message 1 of 8

Skip Read-only or not Modifiable file.

DewayneH
Advocate
Advocate

I have a simple rule to check for and add new properties to existing part and assembly files "Before Save".

Basically, I don't want the properties added to drawings or read-only files.

 

My issue is, I am unable to prevent the properties being written to read-only files.

The suggestion I tried was using "IsModifiable", but it seems to have no affect whether the file is read-only or not.

 

What did I do wrong?

 

Dim doc As Document
doc = ThisApplication.ActiveDocument

'Checks if document is drawing. 
If doc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
'Do Nothing

Else If doc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject And doc.IsModifiable = True Or
doc.DocumentType = DocumentTypeEnum.kPartDocumentObject And doc.IsModifiable = True Then


'Test if properties exist.
Try
	iProperties.Value("Custom", "Component Class") = iProperties.Value("Custom", "Component Class")
	iProperties.Value("Custom", "Spare Part") = iProperties.Value("Custom", "Spare Part")

	'Create properties with default value if they don't exist
Catch
	iProperties.Value("Custom", "Component Class") = "MANUFACTURED"
	iProperties.Value("Custom", "Spare Part") = "NO"

End Try
End If

 

Dewayne
Inventor Pro 2023
Vault Pro 2023
0 Likes
Accepted solutions (2)
1,573 Views
7 Replies
Replies (7)
Message 2 of 8

FINET_Laurent
Advisor
Advisor

Hi,

 

May this helps out : https://forums.autodesk.com/t5/inventor-customization/ilogic-check-if-public-document-is-readonly/td...

 

Regards,

 

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 3 of 8

Sergio.D.Suárez
Mentor
Mentor
Accepted solution

Hi, could you try something similar to what I show you

 

Dim doc As Document = ThisApplication.ActiveDocument

'Checks if document is not drawing. 
If doc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Or _
	doc.DocumentType = DocumentTypeEnum.kPartDocumentObject  Then

	Dim fi As New System.IO.FileInfo(doc.FullFileName)
	If fi.IsReadOnly = False Then
		'Test if properties exist.
		Try
			iProperties.Value("Custom", "Component Class") = iProperties.Value("Custom", "Component Class")
			iProperties.Value("Custom", "Spare Part") = iProperties.Value("Custom", "Spare Part")

			'Create properties with default value if they don't exist
		Catch
			iProperties.Value("Custom", "Component Class") = "MANUFACTURED"
			iProperties.Value("Custom", "Spare Part") = "NO"

		End Try
	End If
End If

 I hope this can help you. Greetings!


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 4 of 8

DewayneH
Advocate
Advocate

Sergio,

You had suggested this method on another post and I tried it when I first created the rule.

It worked great to filter out the read-only files, but on save of new files it throws an error.

"The path is not of a legal form."

I guess the rule is calling file path info that doesn't exist yet.

 

DewayneH_0-1624546445439.png

 

Dewayne
Inventor Pro 2023
Vault Pro 2023
0 Likes
Message 5 of 8

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @DewayneH 

 

Here is a version of @Sergio.D.Suárez 's example that checks if the file has not name first.

 

Also I split up the iProperties into 2 different try/catch statements, this is a bit safer and guards against the issue where the first property exists and the other doesn't, in which case the combined try/catch can still error out.

 

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

 

Dim doc As Document = ThisApplication.ActiveDocument

'Checks if document is not drawing. 
If doc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Or _
	doc.DocumentType = DocumentTypeEnum.kPartDocumentObject  Then

	If doc.FullFileName = "" Then 
		oReadOnly = False 'set flag for files with no name
	Else
		Dim fi As New System.IO.FileInfo(doc.FullFileName)
		oReadOnly = fi.IsReadOnly 'set flag based on file read only
		
	End If
	
	If oReadOnly = False Then
		
		'Test if properties exist.
		Try
			iProperties.Value("Custom", "Component Class") = iProperties.Value("Custom", "Component Class")
		Catch
			iProperties.Value("Custom", "Component Class") = "MANUFACTURED"
		End Try
		
		Try
			iProperties.Value("Custom", "Spare Part") = iProperties.Value("Custom", "Spare Part")
		Catch
			iProperties.Value("Custom", "Spare Part") = "NO"
		End Try
	End If
End If

EESignature

Message 6 of 8

DewayneH
Advocate
Advocate

Checking the file for a name fixed the error.

The read-only files are skipped as originally requested also.

 

Initially, I did have an issue with the properties not writing to assembly files.

I realized there was an "<>" instead of an "=" in the code for the document type.

Once fixed, it works perfect.

 

Thanks to all for the input.

 

If doc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Or _
	doc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then

 

Dewayne
Inventor Pro 2023
Vault Pro 2023
Message 7 of 8

Curtis_Waguespack
Consultant
Consultant

@DewayneH wrote:

 

 

Initially, I did have an issue with the properties not writing to assembly files.

I realized there was an "<>" instead of an "=" in the code for the document type.

Once fixed, it works perfect.

 

If doc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Or _
	doc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then

 


oops! 😕

Sorry about that... I started to change that bit to say "if document type is not equal to drawingobject ... but then changed my mind, but apparently left the <> in there... my apologies for the confusion.

 

If doc.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then

 

 

 

EESignature

0 Likes
Message 8 of 8

DewayneH
Advocate
Advocate

Curtis,

 

The code you help me with has worked flawlessly for a few years now.

Recently, I began to notice an occasional error popup with the rule.

 

"Error on line 37 in rule: Manuf-Commer-Spare, in document:

Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))"

The code is below and line 37 is red.

 

This is only happening with factory files. We don't use them very often which explains why it hasn't been an issue till recently.

I'm currently working with a large batch of iassemblies and am getting it quite frequently.

 

The rule is set to run before save.

The properties are created initially in the factory as expected with no errors and no errors happen if save is clicked manually.

I've also added the property values in the table for all the members.

 

The error only occurs when changing members. If a save is required, the error displays before the save dialogue displays. 

 

Any idea how to prevent the error?

 

Here is the final code you help me with:

'[Rule creates the component classification properties, "Component Class" & "Spare Part" in parts and assemblies.
'Checks if the properties exists, then creates and applies the default value if it doesn't.

'Component Class - Default in parts and assemblies is set according to the files BOM Structure.
'Spare Part - Default is set to "No".

Dim doc As Document = ThisApplication.ActiveDocument



'[Check if document is not drawing. 
If doc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Or _
	doc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then


	'Check if file has file name (for new unsaved files)
	If doc.FullFileName = "" Then
		oReadOnly = False 'set flag for files with no name
	Else
		Dim fi As New System.IO.FileInfo(doc.FullFileName)
		oReadOnly = fi.IsReadOnly 'set flag based on file read only

	End If

	If oReadOnly = False Then

		'Test if properties exist.
		Try
			iProperties.Value("Custom", "Component Class") = iProperties.Value("Custom", "Component Class")
		Catch
			iProperties.Value("Custom", "Component Class") = "MANUFACTURED"
		End Try

		Try
			iProperties.Value("Custom", "Spare Part") = iProperties.Value("Custom", "Spare Part")
		Catch
			iProperties.Value("Custom", "Spare Part") = "NO"
		End Try


		'Set "Component Class" according to Documents BOMStructure

		Dim BOMStructure = doc.ComponentDefinition.BOMStructure

		Select Case BOMStructure

			Case = 51974'Inseparable
				iProperties.Value("Custom", "Component Class") = "MANUFACTURED"

			Case = 51970'Normal
				iProperties.Value("Custom", "Component Class") = "MANUFACTURED"

			Case = 51971'Phantom
				iProperties.Value("Custom", "Component Class") = "PHANTOM"

			Case = 51973'Purchased
				iProperties.Value("Custom", "Component Class") = "COMMERCIAL"

			Case = 51972'Reference
				iProperties.Value("Custom", "Component Class") = "REFERENCE"
		End Select
	End If
End If
']
']

 

Dewayne
Inventor Pro 2023
Vault Pro 2023
0 Likes