iLogic to update iproperties on all open drawings.

iLogic to update iproperties on all open drawings.

floccipier
Advocate Advocate
1,920 Views
17 Replies
Message 1 of 18

iLogic to update iproperties on all open drawings.

floccipier
Advocate
Advocate

Hi, 

spent few hours but I can't seem to find why this rule is not working for all open drawings but works only on single drawing.

For Each oDoc In ThisApplication.Documents.VisibleDocuments
	
	If oDoc.DocumentType = kDrawingDocumentObject Then
	
     Dim oDrawDoc As DrawingDocument = oDoc
	 oDrawDoc.Activate

If (ThisDrawing.ModelDocument Is Nothing) Then Return

modelName = IO.Path.GetFileName(ThisDrawing.ModelDocument.FullFileName)

Try
Part_Material = iProperties.Value(modelName, "Custom", "MATERIAL")

Catch
	Part_Material=""
End Try
Try
Part_Grade = iProperties.Value(modelName, "Custom", "GRADE")
Catch
Part_Grade=""	
End Try
Try
Part_Finish_Type = iProperties.Value(modelName, "Custom", "FINISH TYPE")
Catch
	Part_Finish_Type=""
End Try
Try
Stock_Number = iProperties.Value(modelName, "Project", "Stock Number")
Catch
	Stock_Number =""
End Try

Try
iProperties.Value("Custom", "MATERIAL") = Stock_Number & " - " & Part_Material & " - " & Part_Grade & " || " & Part_Finish_Type
iProperties.Value("Custom", "PROJECT DESCRIPTION") = iProperties.Value(modelName, "Project", "Project")
iProperties.Value("Custom", "PART DESCRIPTION") = iProperties.Value(modelName, "Project", "Description")
iProperties.Value("Custom", "PART NUMBER") = iProperties.Value(modelName, "Project", "Part Number")
iProperties.Value("Custom", "CPT PROJECT") = iProperties.Value(modelName, "Custom", "Company PROJECT")
Catch
End Try
Else
End If
Next oDoc
0 Likes
Accepted solutions (2)
1,921 Views
17 Replies
Replies (17)
Message 2 of 18

floccipier
Advocate
Advocate

@JhoelForshav Sorry to tag you like this - Can you please have look at this whenever you have time, I have modified this so many ways but can't seem to find why it would just pick on drawing but not all of the opened drawings. Thank you. 

0 Likes
Message 3 of 18

WCrihfield
Mentor
Mentor

Is this a 'local' rule, or an external rule?  Is the one drawing it is working on the 'local' drawing, or the drawing that was 'active' when the rule first started?  If so, which? 

If this is a 'local' rule, then the term 'ThisDrawing.ModelDocument' (more specifically the 'ThisDrawing' term) may always be pointing to the 'local' drawing document.  If this is an external rule, and the code is always working for the drawing document that was open/active when the rule first started, it may also be due to using that 'ThisDrawing.ModelDocument' term, because I believe in that case it will always refer to the document that was 'active' when the rule first started.  If any of these scenario's is true, you may have to access the drawing's 'model' document through the oDrawDoc.ReferencedDocuments.Item(1) instead.  Just my initial thoughts.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 18

floccipier
Advocate
Advocate

I though the same and in one (of many) version(s) used oDrawDoc.ReferencedDocuments.Item(1) rather than ThisDrawing.ModelDocument and the result was no different. 😔 It just makes changes on first drawing and ignore all others. 

0 Likes
Message 5 of 18

WCrihfield
Mentor
Mentor

OK.  My next thought is that the 'modelName' variable isn't working for you.  Normally the 'DisplayName' of the document will work here, if it hasn't been altered from default, but I usually avoid doing it that way, due to the stability issue, and opt for accessing the iProperties of other documents the long way, just to be more sure.  So I simply eliminated the use of that variable, in favor of using the actual document variable.  Here's what I've got now, that you can try out.

For Each oDoc As Document In ThisApplication.Documents.VisibleDocuments
	If Not oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then Continue For 'skip to next oDoc
	Dim oDrawDoc As DrawingDocument = oDoc
	oDrawDoc.Activate
	If oDrawDoc.ReferencedDocuments.Count = 0 Then Continue For 'skip to next oDoc
	Dim oMDoc As Document = oDrawDoc.ReferencedDocuments.Item(1)
	Dim oDTProps As PropertySet = oMDoc.PropertySets.Item(3)
	Dim oCProps As PropertySet = oMDoc.PropertySets.Item(4)
	Try
		Part_Material = oCProps.Item("MATERIAL").Value
	Catch
		Part_Material = ""
	End Try
	Try
		Part_Grade = oCProps.Item("GRADE").Value
	Catch
		Part_Grade = ""
	End Try
	Try
		Part_Finish_Type = oCProps.Item("FINISH TYPE").Value
	Catch
		Part_Finish_Type = ""
	End Try
	Try
		Stock_Number = oDTProps.Item("Stock Number").Value
	Catch
		Stock_Number = ""
	End Try
	Try
		'these lines are accessing the iProperties of the 'active' document
		iProperties.Value("Custom", "MATERIAL") = Stock_Number & " - " & Part_Material & " - " & Part_Grade & " || " & Part_Finish_Type
		iProperties.Value("Custom", "PROJECT DESCRIPTION") = oDTProps.Item("Project").Value
		iProperties.Value("Custom", "PART DESCRIPTION") = oDTProps.Item("Description").Value
		iProperties.Value("Custom", "PART NUMBER") = oDTProps.Item("Part Number").Value
		iProperties.Value("Custom", "CPT PROJECT") = oCProps.Item("Company PROJECT").Value
	Catch
	End Try
Next oDoc

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 18

WCrihfield
Mentor
Mentor

@floccipier

I just realized that since those last several lines of code (using iProperties.Value("Custom",....)) are writing the retrieved iProperty values to whatever document is currently 'active', and we are using 'Activate' on each of the documents we are looping through, we are in fact writing the one called "Material" over itself, and the rest are being either updated or created within that newly 'active' document.  Was that your intention?  If not, we may have to convert those lines to using the long route too.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 18

floccipier
Advocate
Advocate
Hi,
Thanks for looking into this - may be this is the issue and no its not my intention infact as you understood I meant to take iprops from each part referenced on each drawing and update that specific drawing with its referenced part model .
0 Likes
Message 8 of 18

WCrihfield
Mentor
Mentor

I was a way for a while, then just came back and looked at this again.  I think I may have been wrong in my last statement.  In those last several lines (of the last code I posted) the first half of each line is referencing the 'active' drawing document in the loop (because they aren't specifying a target document), while the second have of each line is referencing the model document's iProperties (because they are using the PropertySet variables created for the 'model' documet).  It's kind of hart to keep it all straight in my head, because I'm 'multi-tasking' a little too much right now. 😅  Sorry for that mistake...I hope I haven't just confused you even further.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 18

JhoelForshav
Mentor
Mentor
Accepted solution

@floccipier 

I think its better to use API-calls instead of the iLogic interface when you're modifying properties of other documents than the one the rule is running in. It just gives better control of what you're working with IMO.

This rule is quite ugly due to all the try-catch statements and I haven't tried it myself but I think something like this should probably work...

For Each oDoc In ThisApplication.Documents.VisibleDocuments

	If oDoc.DocumentType <> kDrawingDocumentObject Then Continue For
	Dim mDoc As Document
	Try
		mDoc = TryCast(oDoc, DrawingDocument).Sheets(1).DrawingViews(1).ReferencedDocumentDescriptor.ReferencedDocument
	Catch
		Continue For
	End Try
	Dim Part_Material As String
	Dim Part_Grade As String
	Dim Part_Finish_Type As String
	Dim Stock_Number As String
	Dim Company_Project As String

	Dim drawingCprops As PropertySet = oDoc.PropertySets("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
	Dim cProps As PropertySet = mDoc.PropertySets("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
	Dim dProps As PropertySet = mDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")
	Try
		Part_Material = cProps("MATERIAL").Expression
	Catch
	End Try
	Try
		Part_Grade = cProps("GRADE").Expression
	Catch
	End Try
	Try
		Part_Finish_Type = cProps("FINISH TYPE").Expression
	Catch
	End Try
	Try
		Stock_Number = dProps("Stock Number").Expression
	Catch
	End Try
	Try
		Company_Project = cProps("Company PROJECT").Expression
	Catch
	End Try
	Try
		drawingCprops("MATERIAL").Value = Stock_Number & " - " & Part_Material & " - " & Part_Grade & " || " & Part_Finish_Type
	Catch
		drawingCprops.Add(Stock_Number & " - " & Part_Material & " - " & Part_Grade & " || " & Part_Finish_Type, "MATERIAL")
	End Try
	Try
		drawingCprops("PROJECT DESCRIPTION").Value = dProps("Project").Value
	Catch
		drawingCprops.Add(dProps("Project").Value, "PROJECT DESCRIPTION")
	End Try
	Try
		drawingCprops("PART DESCRIPTION").Value = dProps("Description").Value
	Catch
		drawingCprops.Add(dProps("Description").Value, "PART DESCRIPTION")
	End Try
	Try
		drawingCprops("PART NUMBER").Value = dProps("Part Number").Value
	Catch
		drawingCprops.Add(dProps("Part Number").Value, "PART NUMBER")
	End Try
	Try
		drawingCprops("CPT PROJECT").Value = Company_Project
	Catch
		drawingCprops.Add(Company_Project, "CPT PROJECT")
	End Try

Next
Message 10 of 18

floccipier
Advocate
Advocate
thanks for taking time, as long is it would work I find it beautiful not ugly. 🙂

This gives error on
The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.PropertySet.Add(Object PropValue, Object Name, Object PropId)
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)
If looking at error you know what this means please let me know but if you can't it's okay as I understand that you haven't checked it and you might not be able to soon so I will try later to check on my other machine where VS is installed just to debug ilogic.

thanks.
0 Likes
Message 11 of 18

WCrihfield
Mentor
Mentor

FYI...The error indicates that one of the lines where it is trying to 'Add' an iProperty is giving the error, but it doesn't specify which specific line the error occurred on.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 12 of 18

floccipier
Advocate
Advocate
thank you so much - cracking code. The error that was popping up before was not because of problem with your code but it was becuase of problem with my data set I was using before.

Also if you mind me asking - I have noticed that your code solutions to to different issues do continue to be more and more advanced and bit out of norm like this type of assigning I haven't seen before. Would you recommend some resource to learn more robust codes. Thank you so much again.

mDoc = TryCast(oDoc, DrawingDocument).Sheets(1).DrawingViews(1).ReferencedDocumentDescriptor.ReferencedDocument
0 Likes
Message 13 of 18

floccipier
Advocate
Advocate
thanks - you are always helpful and kind and provide me lot of pointers to explain things. really appreciate your help.
0 Likes
Message 14 of 18

floccipier
Advocate
Advocate
On a second thought - since we are using all those try, catch, end try statements then why this error is popping up?
0 Likes
Message 15 of 18

floccipier
Advocate
Advocate
Just wondering that since have used all these try, catch, end try statements then why this error is popping up?
0 Likes
Message 16 of 18

floccipier
Advocate
Advocate
Upon investigating this with debugger I have found that if custom iproperty is not in the drawing, catch part code to add custom property and assigning value won't work and give error. but if cusotm iproperty is already there try part of the code assigns the value successfully and this is the reason it is not giving error on some drawings compared to others.
0 Likes
Message 17 of 18

floccipier
Advocate
Advocate
Sorry to bombard you, further investigation found this about error.
Try
code tries assigning the value of iproperty from part to idw (fails)
Catch
Code assumes that iprops are not in idw template so adds new iproperty and assigns value to idw iproperty and when that iproperty is not available the part it fails again.
End try.

I was wondering how we can catch this error that pops in catch area of code.
0 Likes
Message 18 of 18

JhoelForshav
Mentor
Mentor
Accepted solution

@floccipier 

As long as the property has something to put as its value, the add-method should work. So i just made sure there's always a value there even if the corresponding property doesn't exist in the model document. See if this works for you 🙂

For Each oDoc In ThisApplication.Documents.VisibleDocuments

	If oDoc.DocumentType <> kDrawingDocumentObject Then Continue For
	Dim mDoc As Document
	Try
		mDoc = TryCast(oDoc, DrawingDocument).Sheets(1).DrawingViews(1).ReferencedDocumentDescriptor.ReferencedDocument
	Catch
		Continue For
	End Try
	Dim Part_Material As String = "-"
	Dim Part_Grade As String = "-"
	Dim Part_Finish_Type As String = "-"
	Dim Stock_Number As String = "-"
	Dim Company_Project As String = "-"

	Dim drawingCprops As PropertySet = oDoc.PropertySets("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
	Dim cProps As PropertySet = mDoc.PropertySets("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
	Dim dProps As PropertySet = mDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")
	Try
		Part_Material = cProps("MATERIAL").Expression
	Catch
	End Try
	Try
		Part_Grade = cProps("GRADE").Expression
	Catch
	End Try
	Try
		Part_Finish_Type = cProps("FINISH TYPE").Expression
	Catch
	End Try
	Try
		Stock_Number = dProps("Stock Number").Expression
	Catch
	End Try
	Try
		Company_Project = cProps("Company PROJECT").Expression
	Catch
	End Try
	Try
		drawingCprops("MATERIAL").Value = Stock_Number & " - " & Part_Material & " - " & Part_Grade & " || " & Part_Finish_Type
	Catch
		drawingCprops.Add(Stock_Number & " - " & Part_Material & " - " & Part_Grade & " || " & Part_Finish_Type, "MATERIAL")
	End Try
	Try
		drawingCprops("PROJECT DESCRIPTION").Value = dProps("Project").Value
	Catch
		drawingCprops.Add(dProps("Project").Value, "PROJECT DESCRIPTION")
	End Try
	Try
		drawingCprops("PART DESCRIPTION").Value = dProps("Description").Value
	Catch
		drawingCprops.Add(dProps("Description").Value, "PART DESCRIPTION")
	End Try
	Try
		drawingCprops("PART NUMBER").Value = dProps("Part Number").Value
	Catch
		drawingCprops.Add(dProps("Part Number").Value, "PART NUMBER")
	End Try
	Try
		drawingCprops("CPT PROJECT").Value = Company_Project
	Catch
		drawingCprops.Add(Company_Project, "CPT PROJECT")
	End Try

Next
0 Likes