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 🙂
@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.
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")
SubMainDimcopyFromAsDocument = ThisApplication.Documents.Open("C:\Users\hfljf\Desktop\COPYFROM.ipt", False) 'Document to copy fromDimcopyToAsDocument = ThisApplication.Documents.Open("C:\Users\hfljf\Desktop\COPYTO.ipt", False) 'Document to copy to'Copy the rulesCopyRules(copyFrom, copyTo)
'Copy the Event triggersCopyEventsPropSet(copyFrom, copyTo)
'Close the original documentcopyFrom.Close'Save the document to which rules and triggers has been copiedcopyTo.Save'Close documentcopyTo.CloseEndSubSubCopyRules(CopyFromAsDocument, CopyToAsDocument)
ForEachoRuleAsiLogicRuleIniLogicVb.Automation.Rules(CopyFrom)
DimoCopyAsiLogicRule = iLogicVb.Automation.AddRule(CopyTo, oRule.Name, "")
oCopy.Text = oRule.TextNextEndSubSubCopyEventsPropSet(CopyFromAsDocument, CopyToAsDocument)
DimoPropSetAsInventor.PropertySet = CopyFrom.PropertySets("{2C540830-0723-455E-A8E2-891722EB4C3E}")
TryCopyTo.PropertySets("{2C540830-0723-455E-A8E2-891722EB4C3E}").Delete()
CatchEndTryDimnewPropSetAsInventor.PropertySet = CopyTo.PropertySets.Add("_iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
ForEachpropAsInventor.PropertyInoPropSetnewPropSet.Add(prop.Value, prop.Name, prop.PropId)
NextEndSub
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.
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 🙂
@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.
SubMain ()
DimoFileDlgAsInventor.FileDialog = NothingThisApplication.CreateFileDialog(oFileDlg)
oFileDlg.Filter = "Autodesk Inventor Drawings (*.idw)|*.idw"oFileDlg.DialogTitle = "Select Drawings To With The Rule To Copy"oFileDlg.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePathoFileDlg.MultiSelectEnabled =TrueoFileDlg.FilterIndex = 1
oFileDlg.CancelError = TrueOnErrorResumeNextoFileDlg.ShowOpen()
DimoDrgDocAsDrawingDocumentIfErr.Number <> 0 ThenMsgBox("File not chosen.",,"Dialog Cancellation")
ElseIfoFileDlg.FileName <> ""ThenForEachoFileNameAsStringInoFileDlg.FileName.Split("|")
DimcopyFromAsDocument = ThisApplication.Documents.Open("C:\Users\Joseph\Desktop\Inventor Study\AL WASL\Workspaces\Workspace\Test\Copy\CopyFrom.idw", False) 'Document to copy fromDimcopyToAsDocument = ThisApplication.Documents.Open(oFileName, False) 'Document to copy to'Copy the rulesCopyRules(copyFrom, copyTo)
'Copy the Event triggersCopyEventsPropSet(copyFrom, copyTo)
'Close the original documentcopyFrom.Close'Save the document to which rules and triggers has been copiedcopyTo.Save'Close documentcopyTo.CloseNextEndIfEndSubSubCopyRules(CopyFromAsDocument, CopyToAsDocument)
ForEachoRuleAsiLogicRuleIniLogicVb.Automation.Rules(CopyFrom)
DimoCopyAsiLogicRule = iLogicVb.Automation.AddRule(CopyTo, oRule.Name, "")
oCopy.Text = oRule.TextNextEndSubSubCopyEventsPropSet(CopyFromAsDocument, CopyToAsDocument)
DimoPropSetAsInventor.PropertySet = CopyFrom.PropertySets("{2C540830-0723-455E-A8E2-891722EB4C3E}")
TryCopyTo.PropertySets("{2C540830-0723-455E-A8E2-891722EB4C3E}").Delete()
CatchEndTryDimnewPropSetAsInventor.PropertySet = CopyTo.PropertySets.Add("_iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
ForEachpropAsInventor.PropertyInoPropSetnewPropSet.Add(prop.Value, prop.Name, prop.PropId)
NextEndSub
@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
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.
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
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
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.
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
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
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.
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.
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
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. 👍