i Logic: How to create a variable copy name for an automatically created copy after saving

i Logic: How to create a variable copy name for an automatically created copy after saving

Anonymous
Not applicable
1,064 Views
10 Replies
Message 1 of 11

i Logic: How to create a variable copy name for an automatically created copy after saving

Anonymous
Not applicable

Hello World 🙂

 

I have just found out how to create a copy automatically after saving:

Dim NewDoc As AssemblyDocument
Dim newDocName As String = ThisDoc.Path & "\copyname.iam"

ThisDoc.Document.SaveAs(newDocName, True)
ThisDoc.Launch(newDocName)
NewDoc = ThisApplication.Documents.ItemByName(newDocName)

NewDoc.Save

One question is left:

I don't want to save the copy with a fix copy name, as shown above. The copy name should be variable: In the original document (iam), a form will open automatically and ask for a serial number, that the user has to enter in the form. This serial number should be the copy name of the automatically created copy after saving the original document.

Thank you in advance for any ideas! 🙂

 

0 Likes
Accepted solutions (1)
1,065 Views
10 Replies
Replies (10)
Message 2 of 11

WCrihfield
Mentor
Mentor

So, if you want the new document you are creating to use the serial number that was entered into the form, as its file name, then you should investigate that form, and find out which parameter or iProperty it is storing that information in, and retrieve that data as the value of a variable at the beginning of your code.  Then use that variable when specifying the name of the new file you are about to create.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 11

JelteDeJong
Mentor
Mentor

does this work for you?

Dim doc As PartDocument = ThisDoc.Document


Dim serialNumber As String = InputBox("Enter serial number", "serial number", "")
Dim newDocName As String = IO.Path.Combine(ThisDoc.Path, serialNumber & ".iam")

ThisDoc.Document.SaveAs(newDocName, True)
ThisDoc.Launch(newDocName)

Dim NewDoc As AssemblyDocument = ThisApplication.Documents.ItemByName(newDocName)
NewDoc.Save()

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 4 of 11

Anonymous
Not applicable

Thank you for your reply.

I get the following error message:

 

VentorIn_0-1607458190680.png

 

0 Likes
Message 5 of 11

Anonymous
Not applicable

Thank you for your reply.

 

I did it like that:

Dim NewDoc As AssemblyDocument
Dim newDocName As String = IO.Path.Combine(ThisDoc.Path, serialNumber & ".iam")


ThisDoc.Document.SaveAs(newDocName, True)
ThisDoc.Launch(newDocName)
NewDoc = ThisApplication.Documents.ItemByName(newDocName)

NewDoc.Save

It is possible now, to save the copy with a the entered serial number!

 

But I am facing the following problem now: After entering the serial number, a copy is created immediately. It shouldn't be like that. The copy should be only created after the user clicks on the memory symbol top left.

When I click on the memory symbol, the following error message appears: "Wrong parameter". So, it isn't possible anymore to create a copy after saving the original file. The copy is only created after entering a value in the form "serialnumber".

 

The rule above is dragged and dropped as an event trigger after "After saving document" but it still doesn't work.

0 Likes
Message 6 of 11

WCrihfield
Mentor
Mentor
Accepted solution

I may know why the rule is running when you are not ready for it to run.  It looks like the word "serialNumber" is the name of a local parameter, because it is blue in your post.  Any time you use a local parameter in a local iLogic rule, and its name is blue, that creates a sort of 'trigger' scenario.  Then any time the value of that parameter changes, it will instantly trigger that rule to run.  So when you are entering the value of the "serialNumber" parameter using the form, it will instantly run this rule, because of the way you are using the local parameter within it.  To eliminate this unwanted behavior, you could specify the parameter in a different way, so its name doesn't turn blue.

So you would use it like this:

Parameter("serialNumber")

instead of like this:

serialNumber

That would stop the rule from automatically running when the value of that parameter changes, due to you entering its value from the form.

Also, just in case you weren't aware, using 'True' within the 'SaveAs' sub, causes it to act like 'SaveCopyAs', which saves off a copy of the new document, while the previously active document remains open and active.  If you want the new copy document to be the active document after this line of code, you should use False in the 'SaveAs' sub.  Then you wouldn't have to launch the the newly created copy document, and wouldn't have to specify it from the set of all documents, and wouldn't have to save it, because you already have saved it.

I've included a couple more ideas in this code for you to consider.  There are many ways to do this.  As good practice though, once I've established a document variable, I like to stick with it, rather than redefining the active document multiple times within the same code.  Sometimes that means slightly more code, but that's alright because once the code is working and stable, you don't have to worry about it anymore.  It just does its job in the background, as intended.

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule (" & iLogicVb.RuleName & ") to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oExistingDoc As AssemblyDocument = ThisApplication.ActiveDocument

'the following is retrieving the value of a parameter named "serialNumber" and setting its value
'as the value of a new variable here within this rule
Dim oSN As String = Parameter("serialNumber")
'or
'Dim oSN As String = oExistingDoc.ComponentDefinition.Parameters.Item("serialNumber").Value
'or if "serialNumber" is an iProperty instead of a parameter
'Dim oSN As String = iProperties.Value("Custom","serialNumber")

Dim oNewName As String = IO.Path.GetDirectoryName(oExistingDoc.FullFileName) & "\" & oSN & ".iam"

oExistingDoc.SaveAs(oNewName, True)

Dim oNewDoc As AssemblyDocument = ThisApplication.Documents.Open(oNewName)
oNewDoc.Activate

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

If you have time, please... Vote For My IDEAS 💡and Explore My CONTRIBUTIONS

Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 11

Anonymous
Not applicable

Thank you so much for your detailed response! It worked with your code:

Copy is only created when memory button is clicked and a copy is created with the entered serial number, perfect!

 

May I ask you how to avoid the form "serial number" to pop up in the created copy? It should only pop up in the original file.
I guess, I have to create a new rule that prevent the form "serial number" from appearing in the copy and put it as a trigger in "after saving document". What do you think? But I don't have any idea how that rule can look like to be honest.

0 Likes
Message 8 of 11

WCrihfield
Mentor
Mentor

I'm thinking there must be an iLogic rule that is launching that form.  That rule is likely set-up within your Event Triggers, since it is launching automatically.  Look in your Event Triggers dialog to see which event(s) that rule is listed under.  That will let us know which events are triggering it to run.  If that rule is set-up to run when you save the document (same event you are using to run this other rule), that would be an odd situation to diagnose.  You shouldn't need another rule just to keep the form from showing.  If the form was showing while the rule was still running, we could prevent that using something like this:

iLogicVb.Automation.RulesOnEventsEnabled = False
'your code here
iLogicVb.Automation.RulesOnEventsEnabled = True

but that form is showing after the rule has finished, isn't it?

Just in case it is happening while the rule is still running, here is the same code in which I have used that strategy to avoid the Event Triggers acting while this rule is running.

iLogicVb.Automation.RulesOnEventsEnabled = False
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule (" & iLogicVb.RuleName & ") to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oExistingDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oSN As String = Parameter("serialNumber")
Dim oNewName As String = IO.Path.GetDirectoryName(oExistingDoc.FullFileName) & "\" & oSN & ".iam"

oExistingDoc.SaveAs(oNewName, True)

Dim oNewDoc As AssemblyDocument = ThisApplication.Documents.Open(oNewName)
oNewDoc.Activate
iLogicVb.Automation.RulesOnEventsEnabled = True

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 9 of 11

Anonymous
Not applicable

Thank you again for your help!

I have two rules, that are set-up within my Event-Triggers:

  1. The rule, that contains your code for the copy problem. It is listed under the event “After Save Document”.
  2. The rule that let the form “serialNumber” appear automatically after opening the file:

iLogicForm.Show("serialNumber")

 

 

I have tried to use your code you have suggest me above, like that:

 

iLogicVb.Automation.RulesOnEventsEnabled = False
iLogicForm.Show("serialNumber")
iLogicVb.Automation.RulesOnEventsEnabled = True

 

 

Unfortunately, the form “serialNumber” is not appearing in the original file anymore. I didn’t change anything in the Event-Triggers.

Thank you in advance!

0 Likes
Message 10 of 11

WCrihfield
Mentor
Mentor

OK.  So you must have the rule that shows the form listed under the "After Open Document" event in the Event Triggers.  You shouldn't need to alter that rule at all, so remove those two new beginning and ending lines of code from that rule, and leave it as it was before.  The last code I posted was the rule to copy the assembly document.  I added those two new lines of code to the beginning and end of that rule when I posted it.  That way, neither the event of saving the document, nor the event of opening the document, will cause the Event Triggers to run any rules based those events happening, while the rule is running.  That should have been enough to stop the form from launching while the copy is being made, while not preventing the form from launching as normal for the original document.  Have you tried it that way yet?  Did it not work?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 11 of 11

Anonymous
Not applicable

Alright!

 

I have edit the first rule as it was before, without the two additional lines:

iLogicForm.Show("serialNumber")

Then, I have replaced the second rule with your last code:

 

iLogicVb.Automation.RulesOnEventsEnabled = False
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule (" & iLogicVb.RuleName & ") to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oExistingDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oSN As String = Parameter("serialNumber")
Dim oNewName As String = IO.Path.GetDirectoryName(oExistingDoc.FullFileName) & "\" & oSN & ".iam"

oExistingDoc.SaveAs(oNewName, True)

Dim oNewDoc As AssemblyDocument = ThisApplication.Documents.Open(oNewName)
oNewDoc.Activate
iLogicVb.Automation.RulesOnEventsEnabled = True

 

Now, the form is not appearing in the original file anymore.
Also, the copy can't be created automatically after saving.

 

I am sorry for the circumstances, did I understand something wrong?

 

I have not change anything in the Event-Triggers: First rule is still in Event "After Open Document" and second rule in Event "After Save Document".

 

Thank you!!

0 Likes