Running ipt iproperties from idw

Running ipt iproperties from idw

A.K.CORTEL
Enthusiast Enthusiast
8,575 Views
17 Replies
Message 1 of 18

Running ipt iproperties from idw

A.K.CORTEL
Enthusiast
Enthusiast

Hi forum, Im trying to run ipt iproperties from idw, and I do so successfully by running the rules below.
I have the following rule on part:
iLogic Form.Show("IPROPERTIES")

And the following rule on idw: iLogicVb.RunRule(ThisDrawing.ModelDocument.DisplayName, "I PROPERTIES")

I have four custom parameters on ipt, I can change their value by editing ipt iproperties' from idw, but it want update the changes on part list unless I open the ipt and save it.
Any work around? or suggestions?

0 Likes
Accepted solutions (1)
8,576 Views
17 Replies
Replies (17)
Message 2 of 18

WCrihfield
Mentor
Mentor

Hi @A.K.CORTEL.  If your drawing is referencing the part, the part should already be open.  If not visibly, then invisibly (in the background), so it should not need to be opened.  You may simply need to update the part document and/or the drawing document after making any changes to the part, before the changes will show up in the drawing.  There are several ways to do that, both manually and by code.  However, if your two existing rules only contain the code you have posted above, I don't think you can simply add some extra code to either of them that would accomplish this added behavior.  If you want the update(s) to happen automatically, you would most likely need to take advantage of an event trigger.  Maybe if your parameter edits modify the size of the part, that will cause your drawing view of the part to be updated, and that event of the drawing view being updated could trigger a local/internal iLogic rule within the drawing to run, and within that rule could be some simple code to update the drawing.  You can find this event named "Drawing View Change" on the 'This Document' tab of the Event Triggers dialog, which can be found by clicking on the Event Triggers tool in the iLogic panel of the Manage tab.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 18

A.K.CORTEL
Enthusiast
Enthusiast

Thank you WCrihfield

0 Likes
Message 4 of 18

WCrihfield
Mentor
Mentor

Hi @A.K.CORTEL.  I am not sure if this will work OK for your scenario or not, because there are some variables involved, but this is something I thought of after I got your message.  If your drawing side rule, that runs the part's rule, were to include a bit more code to capture both documents involved to variables, then run the other rule, then include a couple more lines of code to update both documents, this may do what you are wanting.  However, I am not 100% sure if your system will 'wait' for the other rule to run, you to make your changes in the form, then return control back to this rule, to then update the two documents after the changes...or if it will just instantly try to run the two update lines immediately after running the other rule, without waiting for any other processing to happen.

 

You will notice in this rule, that I am using the GetFileName method to get the proper String to use for the RunRule method.  I am doing this, instead of using the model document's DisplayName, because the DisplayName property is a Read\Write property, and is not very reliable.  Sometimes it includes the file extension, and other times it doesn't, and can be changed to something completely different than the file's name.  In many of the iLogic snippets, when you need to specify a referenced document for it to target, it needs to see the file's name, without path, but with file extension, or they will not work.

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MsgBox("A Drawing document must be active for this code to work. Exiting.", vbCritical, "")
	Exit Sub
End If
Dim oDDoc As DrawingDocument = ThisDoc.Document
Dim oMDoc As Document = ThisDoc.ModelDocument
'gets file name, without path, but with file extension
Dim oMFileName As String = System.IO.Path.GetFileName(oMDoc.FullFileName)
iLogicVb.RunRule(oMFileName, "I PROPERTIES")
oMDoc.Update
oDDoc.Update

And if that does not work, because it does not wait for the other stuff to process before doing the updates, there are other ideas.  One of the factors involved here is whether or not your iLogic Form is set to display as 'Modal' or 'Non Modal'.  If it is Modal, the rest of your rule's code (if any) will wait for you to finish using the form, before continuing to run the rest of the rule's code.  But if it is set to non modal, the rest of the rule's code will immediately run, without waiting on you to do anything with the form.  I would advise you to make sure that the form is set to Modal, either by design, or by specifying Modal when you call it to show.  Then on the next line of code after you call it to show (in your rule within the part), add the line to update that part.  And, you could also add more code after that to access the drawing document and update it too.  If you are only changing the values of custom iProperties, and not actually changing any real Parameters directly, I don't think that will trigger any of the standard Event Triggers in the drawing, as I previously hinted at.  I thing you just mixed the terms "iProperties" & "Parameters" in your original post by mistake, which confused me a bit.  You can't really change Parameters values by changing iProperties values, but you can change the values of custom iProperties by changing the values of Parameters, if those custom iProperties were created by you exposing/exporting those Parameters as custom iProperties.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 18

A.K.CORTEL
Enthusiast
Enthusiast

How Crihfield, sorry for my late reply, I thank you for taken the time to help me.
The code didn't work as I was hopping, I also try to use this code but didn't work either:

iLogicVb.RunRule(ThisDrawing.ModelDocument.DisplayName, "I PROPERTIES")
iProperties.Value("Custom", "QTY") = "QTY"
ThisDoc.Document.Update()

Seem like when I trigger it, still I need to roll back for make the code work

0 Likes
Message 6 of 18

A.K.CORTEL
Enthusiast
Enthusiast

I have a part with one custom propertie QTY, the part contains a code: iLogicForm.Show("I PROPERTIES") ThisDoc.Document.Update() Then I have a drawing, my part list contains a propertie QTY, I can run the ipt properties from idw by using the code: iLogicVb.RunRule(ThisDrawing.ModelDocument.DisplayName, "I PROPERTIES") iProperties.Value("Custom", "QTY") = "QTY" ThisDoc.Document.Update() My coal is to edit the ipt properties from idw and I do manage to do so but the custom propertie "QTY" doesn't uptade unless iI roll back to ipt file.

 

Can someone advise?

 

 

0 Likes
Message 7 of 18

WCrihfield
Mentor
Mentor

Hi @A.K.CORTEL.  I am going to attempt another try at this issue for you.  This time I am going to focus on the rule within the part that is used to show the iLogic Form for editing the iProperties.  This code includes some of the stuff I mentioned before, just in case you did not understand or know how to implement the idea.  I included several lines of comments within the code, to sort of explain what is supposed to be going on.  First of all, it tries to show that iLogic Form in 'Modal' mode, instead of 'Non-Modal' mode.  That should cause any other code within that rule, but after that line of code to wait for you to finish using the form, and close it, before the rest of the code will then continue to run.  This is needed, if you want to be able to update the part only after the custom iProperty's value has been edited by the form.  Then it checks to see if the part needs to be updated, and if so, updates it.  Then it gets the current value of that custom iProperty in the part named "QTY" to a variable, so it can be copied to the drawing later in the code.  Then it attempts to get the full path and file name of the drawing document for that part, assuming that it is stored in the same folder as the part, and has the same file name as the part, but with the ".idw" file extension.  If that is not the case, then that part of the code will have to be edited to specify the correct full file name of the drawing.  Then it tries to get and/or open that drawing document.  Then it gets the drawing's custom iProperties set.  Then it tries to get or create a custom iProperty in that set with the same name as the one in the part, and copy the value over to it.  Then it attempts to update and save the drawing, if needed.

Here is the code:

'cause other code to 'wait' for this Form to close, before proceeding
'by showing it as 'Modal'
iLogicForm.Show("I PROPERTIES", FormMode.Modal)
'after the form closes, it will resume with the code below
oDoc = ThisDoc.Document
If oDoc.RequiresUpdate Then oDoc.Update
'get QTY value from this document, to copy to drawing
oQTY = oDoc.PropertySets.Item(4).Item("QTY").Value
'line below does not change file extension of this documet
'just creates a new String value for you
'includes full path, file name, and specified file extension
oIDWFullFileName = ThisDoc.ChangeExtension(".idw")
'use that to get and/or open the drawing document
Dim oIDW As DrawingDocument
Try
	oIDW = ThisApplication.Documents.ItemByName(oIDW)
Catch
	oIDW = ThisApplication.Documents.Open(oIDW, False)
End Try
'get 'Custom' iProperties of drawing document
oIDWCProps = oIDW.PropertySets.Item("Inventor User Defined Properties")
'get or create custom iProperty in the drawing with same name
'then copy the value to the drawing
Dim oIDWCProp As Inventor.Property
Try
	oIDWCProp = oIDWCProps.Item("QTY")
Catch
	oIDWCProp = oIDWCProps.Add(oQTY, "QTY")
End Try
If oIDWCProp.Value <> oQTY Then oIDWCProp.Value = oQTY
If oIDW.RequiresUpdate Then oIDW.Update
If oIDW.Dirty Then oIDW.Save
oIDW = Nothing

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 18

A.K.CORTEL
Enthusiast
Enthusiast

attached files

0 Likes
Message 9 of 18

A.K.CORTEL
Enthusiast
Enthusiast
0 Likes
Message 10 of 18

A.K.CORTEL
Enthusiast
Enthusiast
0 Likes
Message 11 of 18

WCrihfield
Mentor
Mentor
Accepted solution

Hi @A.K.CORTEL.  I don't know if this is any help but I opened and modified the two parts to working as you wanted, then re-saved them.  But since I am using Inventor 2022.3.2 I am not sure if you will be able to open them.  I saw that error too in the new rule of the part.  I was mistakenly using the 'oIDW' variable when trying to specify the full file name of the drawing, when I should have been using the 'oIDWFullFileName' variable in those two places instead.

The two fixed files are attached, if you can use them.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 12 of 18

A.K.CORTEL
Enthusiast
Enthusiast

Hi Crihfield, sorry for bothering you again, every time I save the ipt with a new name I get the same error on line 18...kind of odd

0 Likes
Message 13 of 18

WCrihfield
Mentor
Mentor

Sorry for the delay.  I'm not sure what might be causing the error unless when you save the part with a new file name, there is not also a drawing with that same file name in the same file location as the part.  If that was the case, that would obviously be the problem.  It is trying to get and/or open a drawing file that either does not exist, or can not be found.  If you are using SaveAs for both the part and the drawing, and maintaining matching path & file name for both, then you may need to replace the referenced model on the drawing side, because the drawing may still be pointing to the previous model.  There is a tool on the Manage tab of the drawing called "Replace Model Reference" that is made for that situation.  Whenever I copy a model and drawing file pair within my file explorer screen, then give them new matching file names, I always need to replace the model reference within the drawing, so that it is pointing to the new model.

I slightly modified the code for the rule within the model file, to include a couple more error prevention checks, with some feedback messages, to let you know when those types of problems are encountered.  It now checks to make sure the custom iProperty is found within the model file, and if it is not found, it lets the user know, then exits the rule.  It also checks to make sure the drawing file exists before trying to get it or open it, to avoid that potential error.  If it can't be found, it lets the user know, then exits the rule.

'cause other code to 'wait' for this Form to close, before proceeding
'by showing it as 'Modal'
iLogicForm.Show("I PROPERTIES", FormMode.Modal)
'after the form closes, it will resume with the code below
oDoc = ThisDoc.Document
If oDoc.RequiresUpdate Then oDoc.Update
'get QTY value from this document, to copy to drawing
Dim oQTY As Integer
Try 'just in case it might not exist yet in the model document
	oQTY = oDoc.PropertySets.Item(4).Item("QTY").Value
Catch
	MsgBox("Custom iProperty 'QTY' not found in model.", vbCritical, "")
	Exit Sub
End Try
'line below does not change file extension of this documet
'just creates a new String value for you
'includes full path, file name, and specified file extension
Dim oIDWFullFileName As String = System.IO.Path.ChangeExtension(oDoc.FullFileName, ".idw")
'check to make sure that drawing file exists, before trying to get/open it, to avoid error
If Not System.IO.File.Exists(oIDWFullFileName) Then
	MsgBox("A drawing file with the following FullFileName could not be found:" _
	& vbCrLf & oIDWFullFileName, vbCritical, "")
	Exit Sub
End If
'get and/or open the drawing document
Dim oIDW As DrawingDocument
Try
	oIDW = ThisApplication.Documents.ItemByName(oIDWFullFileName)
Catch
	oIDW = ThisApplication.Documents.Open(oIDWFullFileName, False)
End Try
'get 'Custom' iProperties of drawing document
Dim oIDWCProps As PropertySet = oIDW.PropertySets.Item("Inventor User Defined Properties")
'get or create custom iProperty in the drawing with same name
'then copy the value to the drawing
Dim oIDWCProp As Inventor.Property
Try
	oIDWCProp = oIDWCProps.Item("QTY")
Catch
	oIDWCProp = oIDWCProps.Add(oQTY, "QTY")
End Try
If oIDWCProp.Value <> oQTY Then oIDWCProp.Value = oQTY
If oIDW.RequiresUpdate Then oIDW.Update
If oIDW.Dirty Then oIDW.Save
oIDW = Nothing

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 14 of 18

A.K.CORTEL
Enthusiast
Enthusiast

Hi WCrihfield, no delay at all, I'm not the only one who needs help

I manage to solve the cause of the error by removing the line 30:
If oIDWCProp.Value <> oQTY Then oIDWCProp.Value = oQTY
For some strange reason it was given an error: The change from the string "QTY" to the type 'Double' is not valid.

Then I use the following code for creating a new drawing from templates if the one isn't found:

Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument

Try
ThisApplication.Documents.Open _
(ThisDoc.PathAndFileName(True).Replace("ipt", "idw"))
Return 'exit rule
Catch
End Try

oNewDrawing = ThisDoc.ChangeExtension(".idw")


oTemplateFolder = ThisApplication.FileOptions.TemplatesPath
oTemplate = oTemplateFolder & "Standard.idw"

Dim oDrawingDoc As DrawingDocument
oDrawingDoc = ThisApplication.Documents.Add _
(DocumentTypeEnum.kDrawingDocumentObject, oTemplate, True)

oDrawingDoc.Activate()
Dim oSheet As Sheet
oSheet = oDrawingDoc.Sheets.Item(1)

Dim oPoint As Point2d
oPoint = ThisApplication.TransientGeometry.CreatePoint2d(10, 10)

Dim oView As DrawingView
oView = oSheet.DrawingViews.AddBaseView(oDoc, oPoint, 1, _
ViewOrientationTypeEnum.kFrontViewOrientation, _
DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle)

oDrawingDoc.SaveAs(oNewDrawing, False)

MessageBox.Show("This new file saved as: "& oNewDrawing, "iLogic")


In case of multiply custom entries, how can I deal with that?
And one last thing, I assume the code it wont work for iam, could you please provide a variation for iam as well?
Thang you again Crihfield, you've been a great help!!

0 Likes
Message 15 of 18

WCrihfield
Mentor
Mentor

I see that I had declared the oQTY variable as an Integer, because the value looked like an Integer to me, and when I looked at the iProperties dialog > Custom tab in the model file, that QTY iProperty was listed as 'Number' type, instead of 'Text' type.  But maybe it is better understood as a Double or String, I don't know.  An iProperty's value type can easily change by simply setting a different type of value to it.

 

I do not see anything within this rule that would be unique to only a part, so I see no reason why it could not be used in an assembly too, as long as the main assembly is the model being referred to in the drawing, and not just a component within that assembly.

 

As for multiple 'entries'...if that is needed, and there are many of them, you could create a 'New List(Of String)' (or other similar collection) and put multiple custom iProperty names in that list near where it is first mentioning the oQTY variable in that code for starters.  Then down where I am declaring the 'oIDWCProp' variable, we can enclose that portion, the Try...Catch block and the final 'If <> Then = ' statement all within a loop of each name in that List or collection.  Does that sound like a plan that you could work with?

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 16 of 18

A.K.CORTEL
Enthusiast
Enthusiast
Definitely, I think I can figure it out Thanks
Have a great weekend
0 Likes
Message 17 of 18

A.K.CORTEL
Enthusiast
Enthusiast

Definitely, I think I can figure it out Thanks
Have a great weekend

0 Likes
Message 18 of 18

WCrihfield
Mentor
Mentor

Just in case the List(Of String) idea is not viable, I thought of another idea that might be a bit more useful for creating and updating several custom iProperties.  You could create and use a NameValueMap, then add multiple name & value pairs to it.  The Name side is always a String, but the Value can be many Types of things.  Then loop through that by Item to create/update custom iProperties.  Below is a simple example of this technique.

'prepare a list/map of name and value pairs you want to create custom iProperties for
'you could also use a Dictionary type object, but those are a slightly more complicated to deal with
Dim oMap As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
'these values could be from Parameters, iProperties, or elsewhere, of another document too, instead of hardcoded
oMap.Add("QTY", 10)
oMap.Add("PropName2", "Prop2Value")
oMap.Add("OALength", 25.875)
oMap.Add("Purchased", False)

Dim oDoc As Document = ThisDoc.Document
Dim oCProps As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
For i = 1 To oMap.Count
	Dim oCProp As Inventor.Property = Nothing 'resets value each time
	Try
		'try to find the existing property, and set it as the value of the variable
		oCProp = oCProps.Item(oMap.Name(i))
	Catch
		'not found, so create it
		oCProp = oCProps.Add(oMap.Value(oMap.Name(i)), oMap.Name(i))
	End Try
	'if existing was found, make sure its value is correct
	If oCProp.Value <> oMap.Value(oMap.Name(i)) Then oCProp.Value = oMap.Value(oMap.Name(i))
Next

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes