iLogic event trigering, trying to switch to external rules only

iLogic event trigering, trying to switch to external rules only

M
Contributor Contributor
623 Views
6 Replies
Message 1 of 7

iLogic event trigering, trying to switch to external rules only

M
Contributor
Contributor

Hello

 

I recently switched to 2021 release of Inventor  (from 2015) so I got back to review some of my old iLogic rules.

I've always worked with file templates (part/assemblies) that had at least one rule embeded in the actual file/template trigered before save.

 

I had two seperate rules (part templates) that managed  part naming in Title block, one in regular parts, one in sheet metal parts (adding a suffix of sheet metal extents to the description). So I merged them into one iLogic rule that checks if opened file is a regular or sheet metal part. The rule works correctly for a part file, when the part file is opened directly (as single part file), but the rule does not triger (in the part) from assembly level, even when I "browse through" the assembly tree, to that specific part, and force the rule to run. (It propably checks if the active document is a part/sheet metal, but the program returns that it's an assembly?).

 

1. What I want to achieve for that rule, to convert it to an external rule, that is managed by the new iLogic event trigering functionality, that trigers before save, on all part files, and thus updates file naming (among other things)

One rule that can be run either from the part level itself or assembly level. (Maybe some 'Select case' conditioning? in case the opened file is part doc do this... in case opened as assembly do this... ???)

 

2. an addition the above, a condition for the rule to skip Content Center generated parts (either screws/bolts etc. & Beams user created CC items and so on)

 

I'm not using Vault, never did. In the rule I'm using some iProperities fields and custom user parameters which are already in the template part file.

I want to rid of the embeded ilogic coding from the files, so I also will be able to update old projects to new coding ideas I come up in the future.

 

Any effort regarding my requiest is much appreciated.

 

Current state of the rule is as follows. 

 

On Error Resume Next

Dim curDoc = ThisApplication.ActiveDocument
If curDoc.DocumentType = kPartDocumentObject Then
	'This is a Part Drawing
	If curDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
		'This is a SheetMetal Part
		If SheetMetal.FlatExtentsLength>SheetMetal.FlatExtentsWidth Then
			Parameter("pY")= Round(SheetMetal.FlatExtentsLength,0)
			Parameter("pX")= Round(SheetMetal.FlatExtentsWidth,0)
		Else
			Parameter("pX")= Round(SheetMetal.FlatExtentsLength,0)
			Parameter("pY")= Round(SheetMetal.FlatExtentsWidth,0)
		End If
		Dim oSMDef As SheetMetalComponentDefinition
		oSMDef = curDoc.ComponentDefinition
		oSMDef.FlatPattern.ExitEdit
		iProperties.Value("Project", "Part Number")=iProperties.Value("Project", "Stock Number") & Round(pGrubość,1) & "x" & pX & "x" & pY
	Else
		iProperties.Value("Project", "Part Number")=iProperties.Value("Project", "Stock Number") '& Round(pGrubość,1) & "x" & pX & "x" & pY
	End If
End If

pNr=iProperties.Value("Project", "Description")
pMasa=Round(iProperties.Mass,3)
pObjetosc= Round(iProperties.Volume,6)
pPowierzchnia= Round(iProperties.Area,5)

'Data utworzenia
Rok=Mid(iProperties.Value("Project", "Creation Date"),7,4)
Miesiac=Mid(iProperties.Value("Project", "Creation Date"),4,2)
Nowa_data=Miesiac & "." & Rok
iProperties.Value("Custom", "Data utworzenia")=Nowa_data

iLogicVb.UpdateWhenDone = True

Regards

Marcin.

APDSU 2015-2021
==========================================================
Please use the "Accept as Solution" and "Give Kudos" functions as appropriate to further enhance the value of these forums.
0 Likes
624 Views
6 Replies
Replies (6)
Message 2 of 7

Ralf_Krieg
Advisor
Advisor

Hello

 

The ActiveDocument is the assembly, that's correct. You can use

Dim curDoc = ThisDoc.Document

or

Dim curDoc = ThisApplication.ActiveEditDocument

This should give you a reference to the part document.


R. Krieg
RKW Solutions
www.rkw-solutions.com
Message 3 of 7

WCrihfield
Mentor
Mentor

I am assuming a lot of things in this possible solution, but I think I may have a modified iLogic rule code for you to try.

I realize that your part may sometimes be automatically be saved in the background while saving either an Assembly or a Drawing document, which references it, and that that event may 'trigger' this rule to run, if you have it set up under that event name in the "Parts" tab of the 'Event Triggers' dialog, under the "Before Save Document" event name.  That is an odd situation to attempt to handle from an external rule, without using either a local rule and/or a customized event handler code.  When the rule is triggered from either an Assembly or a Drawing, the 'active document' will almost always be that Assembly or Drawing, so you can't refer that document reference for any Parameters or iProperties.  Anytime you use the [Parameters("param name") = ] call, without specifying a document,  it will attempt to get that parameter from the 'active' document.  Anytime you use the iProperties.Value("set name","prop name") reference, without specifying a document, it attempts to access that property set and property from the 'active' document.

To help avoid that situation, I first check the two different document references (because it is possible for them to be referring to different documents), and check their DocumentType.  If they aren't both referring to the same document, I check each.  If any of them are a Part document, I use that definition and continue with the rest of the code.  If neither are a Part document, I simply exit the rule without any further processing, because I have no reference to an actual part document to work with.

Once I have successfully set the value of the PartDocument variable, I then set-up the document specific variables for the two PropertySet objects we will need to work with later.  It's more stable to access the iProperties this way than using the iProperties.Value() calls and attempting to specify what document to reference within each call.

Anywhere there is an attempt to access a Parameter, I included checks to ensure those parameters exist in the target document, before trying to access/use them, to avoid potential errors.  Same for the one 'custom' iProperty.

I also replaced the SheetMetal.(physicalsize) calls with ones that are document specific.

Then I used a document specific method at the end to update the target document.

Here's the code.  I hope this helps.

'since it is possible for 'ThisApplication.ActiveDocument to not be the same document as ThisDoc.Document,
'and you are concerned about Parts being saved by an Assembly (or a Drawing),
'I would suggest checking the Type of both variations
Dim oPDoc As PartDocument
If ThisApplication.ActiveDocument IsNot ThisDoc.Document Then
	If ThisApplication.ActiveDocumentType = kPartDocumentObject Then
		oPDoc = ThisApplication.ActiveDocument
	ElseIf ThisDoc.Document.DocumentType = kPartDocumentObject Then
		oPDoc = ThisDoc.Document
	Else
		'neither is a Part, so don't run the rule and exit
		Exit Sub 'or Return
	End If
Else
	'both document references refer to the same document
	oPDoc = ThisApplication.ActiveDocument
End If

'prepare iProperty access variables using our oPDoc variable, to avoid using the iProperties.Value() calls
Dim oDTProps As PropertySet = oPDoc.PropertySets.Item("Design Tracking Properties")
Dim oCProps As PropertySet = oPDoc.PropertySets.Item("Inventor User Defined Properties")

'The model will be updated after every Parameter change within the current rule.
Parameter.UpdateAfterChange = True

If oPDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
	'This is a SheetMetal Part
	Dim oFPLength As Double = CDbl(oDTProps.Item("Flat Pattern Length").Value)
	Dim oFPWidth As Double = CDbl(oDTProps.Item("Flat Pattern Width").Value)
	'you might want to check that these parameters exist before trying to access them, to avoid potential errors
	Dim opX, opY As Inventor.Parameter
	Try
		opX = oPDoc.ComponentDefinition.Parameters.Item("pX")
	Catch
		MsgBox("Couldn't find the parameter named 'pX' in the part. Skipping updating it.", , "")
		GoTo OtherProps
	End Try
	Try
		opY = oPDoc.ComponentDefinition.Parameters.Item("pY")
	Catch
		MsgBox("Couldn't find the parameter named 'pY' in the part. Skipping updating it.", , "")
		GoTo OtherProps
	End Try
	
	If oFPLength > oFPWidth Then
		opY.Value = Round(oFPLength, 0)
		opX.Value = Round(oFPWidth, 0)
	Else
		opX.Value = Round(oFPLength, 0)
		opY.Value = Round(oFPWidth, 0)
	End If
	'What is "pGrubość"?  If it is a 'local' parameter's name, you need to specify it in a different way in an external rule
	'assuming it is a parameter's name, and that it is numeric type, attempting to make sure it exists, before attempting to access it
	Dim opGrubosc As Double
	Try
		opGrubosc = oPDoc.ComponentDefinition.Parameters.Item("pGrubość").Value
	Catch
		MsgBox("Couldn't find a parameter named 'pGrubość' in the part.  Skipping updating Part Number value.", , "")
		GoTo OtherProps
	End Try
	oDTProps.Item("Part Number").Value = oDTProps.Item("Stock Number").Value & Round(opGrubosc,1) & "x" & pX & "x" & pY
Else
	oDTProps.Item("Part Number").Value = oDTProps.Item("Stock Number").Value
End If

OtherProps :
pNr = oDTProps.Item("Description").Value
pMasa = Round(CDbl(oDTProps.Item("Mass").Value), 3)
pObjetosc = Round(CDbl(oDTProps.Item("Volume").Value), 6)
pPowierzchnia = Round(CDbl(oDTProps.Item("SurfaceArea").Value), 5)

'Data utworzenia 'utworzenia = create
Dim Rok As Integer = CDate(oDTProps.Item("Creation Time").Value).Year 'rok = year
Dim Miesiac As Integer = CDate(oDTProps.Item("Creation Time").Value).Month 'meisiac = month
Dim Nowa_data As String = Miesiac & "." & Rok 'nowa = new
'make sure this custom iProperty exists, before attempting to access it
Dim oCProp As Inventor.Property
Try
	oCProp = oCProps.Item("Data utworzenia")
	oCProp.Value = Nowa_data
Catch
	oCProp = oCProps.Add(Nowa_data, "Data utworzenia")
End Try
oPDoc.Update

 

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 7

M
Contributor
Contributor

Thank You very much for Your fast reply, and for the full modified code. All of Your assumptions are correct.

Parameters below are all "custom" parameters/iProperities, defined in all my file templates wchich I use (or have them avaiable to be extracted if needed) in drawing Titleblock or symbols. All of them are part specific

pX, pY, pZ, pGrubość (Thickness), pNr, pMasa, pObjetosc, pPowierzchnia - for parts template
pNr, pMasa, pObjetosc, pPowierzchnia - for assembly file template

I'll try to make an assembly as soon as I have some time avaiable for testing the updated iLogic via event trigering.

 

Regards

Marcin

APDSU 2015-2021
==========================================================
Please use the "Accept as Solution" and "Give Kudos" functions as appropriate to further enhance the value of these forums.
0 Likes
Message 5 of 7

M
Contributor
Contributor

Hello

 

The assembly level trigering seems to work, but the rule itself gives wrong output on sheet metal parts.

When I run Your code on single part, it launches, but the end result for Sheet Metal Part is wrong, the description is not as it should. See the attatched ipt, with my original local rule.

When my original rule is run the description (iProps Part Number field) reads

Plate 3x500x1000

when Your code is run, the field reads

Plate 0,3xx

The first parameter pGrubość (Thickness) has decimal place off by one, and there are no sheet metal extents values present, should be metric values in millimeters.

 

 

Inventor Localization is Polish

 

APDSU 2015-2021
==========================================================
Please use the "Accept as Solution" and "Give Kudos" functions as appropriate to further enhance the value of these forums.
0 Likes
Message 6 of 7

M
Contributor
Contributor

Edit: 

It seems all the SM_ parameters, except SM_Thickness are in centimeters.

You also tweaked the date formatting, could You refine it so it shows months as two digit value.

 

 

 

APDSU 2015-2021
==========================================================
Please use the "Accept as Solution" and "Give Kudos" functions as appropriate to further enhance the value of these forums.
0 Likes
Message 7 of 7

WCrihfield
Mentor
Mentor

Hi @M.  There is something strange going on in the part file you sent me.  The value of the 'Part Number' iProperty and value of the custom iProperty called "Data utworzenia", both are acting strange.  I can display the date I'm about to write to them in a MsgBox, and it looks correct, but when it writes the data to those properties, it doesn't look correct in the dialog box afterwords.

Here is the updated iLogic rule code:

Sub Main
	'since it is possible for 'ThisApplication.ActiveDocument to not be the same document as ThisDoc.Document,
	'and you are concerned about Parts being saved by an Assembly (or a Drawing),
	'I would suggest checking the Type of both variations
	Dim oPDoc As PartDocument
	If ThisApplication.ActiveDocument IsNot ThisDoc.Document Then
		If ThisApplication.ActiveDocumentType = kPartDocumentObject Then
			oPDoc = ThisApplication.ActiveDocument
		ElseIf ThisDoc.Document.DocumentType = kPartDocumentObject Then
			oPDoc = ThisDoc.Document
		Else
			'neither is a Part, so don't run the rule and exit
			Exit Sub 'or Return
		End If
	Else
		'both document references refer to the same document
		oPDoc = ThisApplication.ActiveDocument
	End If
	Dim oParams As Inventor.Parameters = oPDoc.ComponentDefinition.Parameters
	
	'prepare iProperty access variables using our oPDoc variable, to avoid using the iProperties.Value() calls
	Dim oDTProps As PropertySet = oPDoc.PropertySets.Item("Design Tracking Properties")
	Dim oCProps As PropertySet = oPDoc.PropertySets.Item("Inventor User Defined Properties")
	
	'used in both document SubTypes, so defined before SubType check block of code
	Dim oStockNum As String = oDTProps.Item("Stock Number").Value
	
	If oPDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
		'This is a SheetMetal Part
		Dim oFPLength As Double = CDbl(oDTProps.Item("Flat Pattern Length").Value)
		oFPLength = ConvertToMM(Round(oFPLength, 0))
		'MsgBox("oFPLength = " & oFPLength,,"")
		Dim oFPWidth As Double = CDbl(oDTProps.Item("Flat Pattern Width").Value)
		oFPWidth = ConvertToMM(Round(oFPWidth, 0))
		'MsgBox("oFPWidth = " & oFPWidth,,"")
		
		'checking that these parameters exist before trying to access them, to avoid potential errors
		Dim opX, opY As Inventor.Parameter
		Try
			opX = oParams.Item("pX")
		Catch
			MsgBox("Couldn't find the parameter named 'pX' in the part. Skipping updating it.", , "")
			GoTo OtherProps
		End Try
		Try
			opY = oParams.Item("pY")
		Catch
			MsgBox("Couldn't find the parameter named 'pY' in the part. Skipping updating it.", , "")
			GoTo OtherProps
		End Try
		opX.Value = Min(oFPWidth,oFPLength)
		opY.Value = Max(oFPWidth, oFPLength)
		oPDoc.Update

		'MsgBox("opX.Value = " & opX.Value & " and opY.Value = " & opY.Value,,"")

		'attempting to make sure it exists, before attempting to access it
		Dim opGrubosc As Double
		Try
			opGrubosc = ConvertToMM(oParams.Item("pGrubość").Value)
			opGrubosc = Round(opGrubosc, 1)
			'MsgBox("opGrubosc = " & opGrubosc,,"")
		Catch
			MsgBox("Couldn't find a parameter named 'pGrubość' in the part.  Skipping updating Part Number value.", , "")
			GoTo OtherProps
		End Try
		Dim oThickness As String = CStr(opGrubosc)
		Dim oXStr As String = CStr(Min(oFPWidth,oFPLength))
		Dim oYStr As String = CStr(Max(oFPWidth,oFPLength))
		'MsgBox("oXStr = " & oXStr & " and oYStr = " & oYStr,,"")
		Dim oPN As String = oStockNum & oThickness & "x" & oXStr & "x" & oYStr
		MsgBox("oPN = " & oPN, , "") 'to show you what it should look like
		oDTProps.Item("Part Number").Value = oPN
		oPDoc.Update
	Else
		oDTProps.Item("Part Number").Value = oStockNum
		oPDoc.Update
	End If
	
	'can't refer to these parameters by their local 'unquoted' names in external rules
	Dim opNr, opMasa, opObjetosc, opPowierschnia As Inventor.Parameter
	OtherProps :
	Try
		opNr = oParams.Item("pNr")
		opMasa = oParams.Item("pMasa")
		opObjetosc = oParams.Item("pObjetosc")
		opPowierschnia = oParams.Item("pPowierzchnia")
	Catch
		MsgBox("One of the following Parameters was not found:  'pNr', 'pMasa', 'pObjetosc', 'pPowierzchnia'" & vbCrLf & _
		"Skipping to the 'Data utworzenia' property's value.", , "")
		GoTo CreationDate
	End Try
	opNr.Value = oDTProps.Item("Description").Value
	opMasa.Value = Round(CDbl(oDTProps.Item("Mass").Value), 3)
	opObjetosc.Value = Round(CDbl(oDTProps.Item("Volume").Value), 6)
	opPowierschnia.Value = Round(CDbl(oDTProps.Item("SurfaceArea").Value), 5)
	oPDoc.Update
	
	CreationDate :
	Dim oCreationDate As Date = CDate(oDTProps.Item("Creation Time").Value)
	Dim Nowa_data As String = Strings.Format(oCreationDate, "MM.yyyy")
	MsgBox("Nowa_data = " & Nowa_data, , "") 'to show you what it should look like
	
	'make sure this custom iProperty exists, before attempting to access it
	Dim oCProp As Inventor.Property
	Try
		oCProp = oCProps.Item("Data utworzenia")
		oCProp.Value = Nowa_data
		oCProp.Expression = Nowa_data
	Catch
		oCProp = oCProps.Add(Nowa_data, "Data utworzenia")
	End Try
	oPDoc.Update
End Sub

Function ConvertToMM(oInputValue As Double) As Double
	ConvertToMM = ThisApplication.UnitsOfMeasure.ConvertUnits(oInputValue, UnitsTypeEnum.kDatabaseLengthUnits, UnitsTypeEnum.kMillimeterLengthUnits)
End Function

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)

0 Likes