Copy ilogic rules between two files

Copy ilogic rules between two files

martinkasparides
Participant Participant
3,097 Views
22 Replies
Message 1 of 23

Copy ilogic rules between two files

martinkasparides
Participant
Participant

Hi,

 

please, I don't know, how I can copy ilogic rules from template drawing to other drawing files using ilogic (VB.net API). And if it's possible, how i can set copied ilogic rule to event run after save file.

 

In my program, I am changing title block in old drawings to a new block from template, but i need to old drawing copy three ilogic rules and set event.

 

Thank's a lot, sorry for my English, I'm a beginner 🙂

0 Likes
Accepted solutions (2)
3,098 Views
22 Replies
Replies (22)
Message 21 of 23

e_frissell
Advocate
Advocate

@Curtis_Waguespack

This is kinda funny but when I run that rule it loses the names of the rules in the iLogic Event Triggers.  Previously those rules were Scale and Date Stamp.  The rule seems to fail at CopyTo.Save and I'd assume it's because of the rules got renamed to "" in the Event Trigger.

 

If it got to CopyTo.Save it should have copied the form however the iLogic code hasn't copied the form.

 

The copying of the rules got a lot faster some how?

 

e_frissell_1-1689275041093.png

 

0 Likes
Message 22 of 23

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @e_frissell 

 

I didn't actually test the rules and event trigger stuff earlier, I just commented that out at the time because my example file didn't have any rule or event triggers present.

 

But I just did a very quick test and everything worked as expected, except I did run into an issue with the Form browser not updating, so I added @WCrihfield's blink sub to this.

 

I also noticed that it would make multiple copies of the rule if it was already present, so I added a line to delete the existing and replace it. 

 

Also changed the goto, to use continue for. 

 

Anyway, this version works from me. It copies the rules and event triggers. I don't see the blank names or get the errors. I'm running Inventor 2023 at the moment.

 

Sub Main
	Dim copyFrom As Document = ThisApplication.Documents.Open("myTemplateLocation", False) 'Document to copy from
	Dim copyTo As Document = ThisDoc.Document    'Document to copy to
	'Copy the rules
	CopyRules(copyFrom, copyTo)

	'Copy the Event triggers
	CopyEventsPropSet(copyFrom, copyTo)

	' Copy form
	CopyForm(copyFrom, copyTo)
	
	Call BlinkTree

	'Save the document to which rules and triggers has been copied
	copyTo.Save
	copyFrom.close

End Sub

Sub CopyRules(CopyFrom As Document, CopyTo As Document)

	For Each oRule As iLogicRule In iLogicVb.Automation.Rules(CopyFrom)
		
		'try to delete the rule if it exists ( so it can be added again)
		Try: iLogicVb.Automation.DeleteRule(CopyTo, oRule.Name) :Catch: End Try
		
		If oRule.Name = "Scale" Then Continue For
		If oRule.Name = "Date Stamp" Then Continue For
		
		Dim oCopy As iLogicRule = iLogicVb.Automation.AddRule(CopyTo, oRule.Name, "")
		oCopy.Text = oRule.Text
	Next
End Sub

Sub CopyEventsPropSet(CopyFrom As Document, CopyTo As Document)
	Dim oPropSet As Inventor.PropertySet = CopyFrom.PropertySets("{2C540830-0723-455E-A8E2-891722EB4C3E}")
	Try
		CopyTo.PropertySets("{2C540830-0723-455E-A8E2-891722EB4C3E}").Delete()
	Catch
	End Try
	Dim newPropSet As Inventor.PropertySet = CopyTo.PropertySets.Add("_iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
	For Each prop As Inventor.Property In oPropSet
		newPropSet.Add(prop.Value, prop.Name, prop.PropId)
	Next
End Sub
Sub CopyForm(CopyFrom As Document, CopyToDoc As Document)

	Dim Attset As Inventor.AttributeSet
	For Each Attset In CopyFrom.AttributeSets
		If Attset.Name Like "iLogicInternalUi*" Then
			Try
				Attset.CopyTo(CopyToDoc)
			Catch ex As Exception
				Logger.Error(ex.Message)
			End Try
		End If
	Next

End Sub

Sub BlinkTree
	'toggle visibility Of iLogic dockable window To update it
	For Each oDW As Inventor.DockableWindow In ThisApplication.UserInterfaceManager.DockableWindows
		If oDW.InternalName = "ilogic.treeeditor" Then
			oDW.Visible = False
			oDW.Visible = True
		End If
	Next
End Sub

 

EESignature

Message 23 of 23

e_frissell
Advocate
Advocate

Removed the copyTo.Save, moved the continue for lines up above the try and now it works great!  Thanks for adding in the blink, I would have had no clue where to start for that.

 

Great job @Curtis_Waguespack that's really impressive!