Copy ilogic rules between two files

Copy ilogic rules between two files

martinkasparides
Participant Participant
3,078 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,079 Views
22 Replies
Replies (22)
Message 2 of 23

FINET_Laurent
Advisor
Advisor

Morning,

 

What about creating external rules  ?

 

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 23

Anonymous
Not applicable

@martinkasparides  I think copying ilogic code from one file to other is not easy. There is someting they called code injector but I haven't tried it myself. Please check if this is what you are looking for.

 

https://forums.autodesk.com/t5/inventor-forum/code-injector/td-p/7070981 

0 Likes
Message 4 of 23

JhoelForshav
Mentor
Mentor

Hi @martinkasparides 

I'm not sure where how you want to run the rule that copies the rules and triggers from the template. But I made an example for you and hopefully you can tweak it to fit your needs. Just put your document paths to the document to copy from and copy in the rule instead of my test paths and see how it works for you 🙂

 

This rule will copy all the rules and event triggers from one document ("copyFrom") to another document ("copyTo")

Sub Main
	Dim copyFrom As Document = ThisApplication.Documents.Open("C:\Users\hfljf\Desktop\COPYFROM.ipt", False) 'Document to copy from
	Dim copyTo As Document = ThisApplication.Documents.Open("C:\Users\hfljf\Desktop\COPYTO.ipt", False) 'Document to copy to
	'Copy the rules
	CopyRules(copyFrom, copyTo)
	'Copy the Event triggers
	CopyEventsPropSet(copyFrom, copyTo)
	'Close the original document
	copyFrom.Close
	'Save the document to which rules and triggers has been copied
	copyTo.Save
	'Close document
	copyTo.Close
End Sub
Sub CopyRules(CopyFrom As Document, CopyTo As Document)
	For Each oRule As iLogicRule In iLogicVb.Automation.Rules(CopyFrom)
		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
Message 5 of 23

Anonymous
Not applicable

It works fine.

 

 

Message 6 of 23

martinkasparides
Participant
Participant

Hi, thank you for reply. Your solution looks fine, but i have a (beginner) problem. I need this code apply in my API and I have problem to transform to vb.net. I tried it, but .... ilogicVb is not declared. I Imports two dll's Imports Autodesk.iLogic.Automation and Autodesk.iLogic.Interfaces.

 

Thanks again for your help

0 Likes
Message 7 of 23

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @martinkasparides 

I put together a quick VS forms app for you. See attached rar-archive. I made a simple form to copy the rules and event triggers. You can see in the code how i obtained the ilogic automation object. hope it helps 🙂

Message 8 of 23

martinkasparides
Participant
Participant

You are the best! Thanks a lot. Have a nice day

Message 9 of 23

Anonymous
Not applicable

@JhoelForshav  I'm taking this opportunity to learn. You gave me before the solution of pdffill, from that code I took the same concept and made it work somehow. Need your comment how to improve.

Thank you, I learned many things from you and the rest of the top solution authors here.  

 

Sub Main ()
	Dim oFileDlg As Inventor.FileDialog = Nothing
	ThisApplication.CreateFileDialog(oFileDlg)
	oFileDlg.Filter = "Autodesk Inventor Drawings (*.idw)|*.idw"
	oFileDlg.DialogTitle = "Select Drawings To With The Rule To Copy"
	oFileDlg.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
	oFileDlg.MultiSelectEnabled =True 
	oFileDlg.FilterIndex = 1
	oFileDlg.CancelError = True
	On Error Resume Next
	oFileDlg.ShowOpen()
	Dim oDrgDoc As DrawingDocument
	If Err.Number <> 0 Then
		MsgBox("File not chosen.",,"Dialog Cancellation")
	ElseIf oFileDlg.FileName <> "" Then
		For Each oFileName As String In oFileDlg.FileName.Split("|")
			Dim copyFrom As Document = ThisApplication.Documents.Open("C:\Users\Joseph\Desktop\Inventor Study\AL WASL\Workspaces\Workspace\Test\Copy\CopyFrom.idw", False) 'Document to copy from
			Dim copyTo As Document = ThisApplication.Documents.Open(oFileName, False) 'Document to copy to
			'Copy the rules
			CopyRules(copyFrom, copyTo)
			'Copy the Event triggers
			CopyEventsPropSet(copyFrom, copyTo)
			'Close the original document
			copyFrom.Close
			'Save the document to which rules and triggers has been copied
			copyTo.Save
			'Close document
			copyTo.Close
			
		Next
		
	End If
End Sub



Sub CopyRules(CopyFrom As Document, CopyTo As Document)
	For Each oRule As iLogicRule In iLogicVb.Automation.Rules(CopyFrom)
		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

 

0 Likes
Message 10 of 23

e_frissell
Advocate
Advocate

@JhoelForshav this is a pretty cool implementation to copy rules from 1 file to another, but I was wondering if it was possible to take it one step further and get a form from a source doc and copy that to your destination?

 

I added the below but once you get to the Automation there's nothing that references a form

 

Alternatively is it possible to use iLogic to create a form?  Or have a global form that can be accessed and copied into a destination document?

 

 

Sub CopyForm(CopyFrom As Document, CopyTo As Document)
	For Each oForm As iLogicForm In iLogicVb.Automation. 'Nothing exists beyond this point

 

 

0 Likes
Message 11 of 23

WCrihfield
Mentor
Mentor

Hi @e_frissell.  There is currently not any easy way to do that by code.  It is certainly doable manually though.  When you create an internal iLogic form (saved within the document), a form specification is created, and the data for that form specification seems to be stored within the document attributes.  A new AttributeSet, and a new Attribute are added to the document, and the data for that form specification seems to be stored to the Value of the Attribute as a Byte Array.  There is a pretty complicated way of digging into some of the other iLogic resource references that are not normally included in regular rules, and a way to access these form specifications, but I haven't gotten to the point of being able to copy them from one document to another yet.  Were close though.  We can delete the forms you see in your Forms tab by deleting the AttributeSet / Attribute that are attached to the document, and whose name starts with specific text.  I am not sure if that totally deletes them, or just causes them to no longer be visible, and may be messing something up behind the scenes.  We just got the ability to close an open iLogic Form this year, so something like this may be coming soon.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 12 of 23

e_frissell
Advocate
Advocate

Shame.  Saw an old post of yours that mentioned being unable to copy forms, but saw that you could right click and copy a form and that got me wondering if there'd been updates on this front

 

This might be wild but what's the chance that iLogic can replicate mouse clicks?  First macro I ever made was about a decade ago in Solidworks to swap out templates and that was made with the macro recorder which automated the clicks and the location of each click since the dialog box pop ups never changed.  It might be possible to replicate this however the lack of a macro recorder would make this way more difficult

0 Likes
Message 13 of 23

Curtis_Waguespack
Consultant
Consultant

Hi @e_frissell ,

 

I did just a quick test and this adaption from the above examples seemed to work.

 

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

 

AddReference "Autodesk.iLogic.Core.dll"
AddReference "Autodesk.iLogic.UiBuilderCore.dll"
Imports iLogicCore = Autodesk.iLogic.Core
Sub Main
	Dim oFileDlg As Inventor.FileDialog = Nothing
	ThisApplication.CreateFileDialog(oFileDlg)
	oFileDlg.Filter = "Inventor Files (*.dwg;*.idw;*.iam;*.ipt)|*.dwg;*.idw;*.iam;*.ipt|All Files (*.*)|*.*"
	oFileDlg.DialogTitle = "Select File"
	oFileDlg.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
	oFileDlg.MultiSelectEnabled = False
	oFileDlg.FilterIndex = 1
	oFileDlg.CancelError = True
	On Error Resume Next
	oFileDlg.ShowOpen()
	Dim oDrgDoc As DrawingDocument
	If Err.Number <> 0 Then
		MsgBox("File not chosen.", , "Dialog Cancellation")
	ElseIf oFileDlg.FileName <> "" Then
		For Each oFileName As String In oFileDlg.FileName.Split("|")
			Dim copyToDoc As Document = ThisApplication.Documents.Open(oFileName, False) 'Document to copy to
			'Copy the forms
			CopyForms(copyToDoc)
			'Save the document to which the forms were copied
			copyToDoc.Save
			'Close document
			copyToDoc.Close
		Next

	End If
End Sub



Sub CopyForms(CopyToDoc As Document)

	Dim oDoc = ThisApplication.ActiveDocument

	iCount = 0
	Dim oUIatts As New iLogicCore.UiBuilderStorage.UiAttributeStorage(oDoc)
	For Each oName In oUIatts.FormNames
		'  Dim oFormsSpecs = oUIatts.LoadFormSpecification(oName)
		iCount = iCount + 1
	Next

	If iCount = 0 Then
		Exit Sub
	End If

	Dim Attset As Inventor.AttributeSet

	For Each Attset In oDoc.AttributeSets
		If Attset.Name Like "iLogicInternalUi*" Then
			Attset.CopyTo(CopyToDoc)
		End If
	Next
End Sub

 

EESignature

Message 14 of 23

WCrihfield
Mentor
Mentor

It might be possible to simulate a series of mouse clicks that way in Inventor, but I have not personally created any automation solutions that way.  I have used the InteractionEvents and MouseEvents to capture mouse pointer position when the mouse is clicked on several occasions, but not the other way around.  Plus using the InteractionEvents is not very convenient.  Plus, since the main iLogic tab can be moved around on the screen, and the whole DockableWindow area can be hidden, resized, scrolled, deactivated (other sub tab activated), getting the exact location of an internal iLogic Form's user interface area on the screen reliably would be pretty challenging.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 15 of 23

e_frissell
Advocate
Advocate

Whoa, there's a lot to unpack with that and frankly I'm not sure where to start haha your iLogic knowledge never ceases to amaze.  If you don't mind I'd like to post up what I'm running and could use some help integrating that into my rule.  Location would be as called out in sub Main

 

Sub Main
	Dim copyFrom As Document = ThisApplication.Documents.Open("myTemplateLocation", False) 'Document to copy from
	Dim copyTo As Document = ThisDoc.Document   's.Open("C:\Users\hfljf\Desktop\COPYTO.ipt", False) 'Document to copy to
	'Copy the rules
	CopyRules(copyFrom, copyTo)
	
	'Copy the Event triggers
	CopyEventsPropSet(copyFrom, copyTo)
	' ------ COPY FORM HERE -------
	' Copy form
	' CopyForm(copyFrom, copyTo) ' Copy form as subroutine
	'iLogicVb.Automation.RunExternalRule(CopyForm) ' Copy form as external rule
	' ------ End copy form -------
	
	'Save the document to which rules and triggers has been copied
	copyTo.Save

End Sub

Sub CopyRules(CopyFrom As Document, CopyTo As Document)
	For Each oRule As iLogicRule In iLogicVb.Automation.Rules(CopyFrom)
		If oRule.Name = "Scale" Then GoTo Skip :
		If oRule.Name = "Date Stamp" Then GoTo Skip :
		Dim oCopy As iLogicRule = iLogicVb.Automation.AddRule(CopyTo, oRule.Name, "")
		oCopy.Text = oRule.Text
		Skip :
		
	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, CopyTo As Document)
	'For Each oForm As iLogicForm In iLogicVb.Automation.
End Sub

 

0 Likes
Message 16 of 23

WCrihfield
Mentor
Mentor

Cool.  I was hoping it was going to be that simple, but I simply had not tried it yet.  I just created a simple rule of my own to test with, and got an odd result.  When I first ran the rule, the other document was created and visibly shown on my screen.  Then I clicked into its Forms tab, and it was not showing anything.  Then I want back to the original document and looked at the sample Form there, and that was still there, as expected.  Then I went back to the new document and the form was now showing in its Forms tab.  This too must need a 'blink' type helper (toggle visibility of the iLogic DockableWindow).  When I tried it again, if I stay within that new document, looking at the empty Forms tab, then click the 'x' to close the main iLogic tab, then click the '+' to show it again, the form then shows up.  Similar situation to the 'delete forms' scenario.

Dim oFromDoc As Document = ThisDoc.Document
Dim oSets As AttributeSets = oFromDoc.AttributeSets
If oSets.Count = 0 Then Exit Sub
Dim oToDoc As Document = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject)
For Each oSet As AttributeSet In oSets
	'the AttributeSet.Name should start with "iLogicInternalUi"
	If oSet.Name Like "iLogicInternalUi*" Then
		oSet.CopyTo(oToDoc, False)
	End If
Next
'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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 17 of 23

WCrihfield
Mentor
Mentor

If I create the other new document 'invisible', then switch to that document by clicking on the view tab at the bottom, the form is already there, so it's not really much of a problem.  The reason I did not specify a specific template was that I wanted to test the whole DocumentInterest thing.  When creating the Event Properties PropertySet within another document, the other document usually needs to have a DocumentInterest for the iLogic ApplicationAddin, so that the event triggering will work properly after the fact.  That doesn't seem to be a problem in this case.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 18 of 23

Curtis_Waguespack
Consultant
Consultant

Hi @e_frissell , Give this a try.

 

From my quick and dirty testing, I think this might fail if the CopyTo document already has one or more forms in it.

 

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)

	'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)
		If oRule.Name = "Scale" Then GoTo Skip :
		If oRule.Name = "Date Stamp" Then GoTo Skip :
		Dim oCopy As iLogicRule = iLogicVb.Automation.AddRule(CopyTo, oRule.Name, "")
		oCopy.Text = oRule.Text
		Skip :

	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

 

EESignature

0 Likes
Message 19 of 23

Curtis_Waguespack
Consultant
Consultant

@WCrihfield wrote:

If I create the other new document 'invisible', then switch to that document by clicking on the view tab at the bottom, the form is already there, so it's not really much of a problem.  


 

This and what you mentioned previously was what I was seeing also.

 

What I'd really like to be able to do is copy a specific form by name. There is a SaveFormSpecification call that might do it, but I don't have time to dig into it at the moment.

 

Curtis_Waguespack_0-1689274149087.png

 

 

 

AddReference "Autodesk.iLogic.Core.dll"
AddReference "Autodesk.iLogic.UiBuilderCore.dll"
Imports iLogicCore = Autodesk.iLogic.Core

	Dim oUIatts As New iLogicCore.UiBuilderStorage.UiAttributeStorage(ThisDoc.Document)
	For Each oName In oUIatts.FormNames
		Logger.Info(oName)		
		If oName = "Best Form Ever" Then
			Dim oFormsSpecs = oUIatts.LoadFormSpecification(oName)
			'oUIatts.SaveFormSpecification()
		End If
	Next

 

 

EESignature

Message 20 of 23

WCrihfield
Mentor
Mentor

Me too. 😁  I have already been able to dig into the individual controls within the forms, but its all still a bit more complicated than I had hoped for.  And haven't found the time to dig deeper yet.  Fairly optimistic about it though.  And every one of these steps along the way helps. 👍

Wesley Crihfield

EESignature

(Not an Autodesk Employee)