Macro/script - undo in one step

Hello everyone,

I have a quick question about a script / macro that I use to load standard parts from our library:

!
EchoAus
-_Import
R:\Bibliothek\Schrauben\M8_Innensechskant\30.3dm
_Enter
_ChangeToCurrentLayer
_-Layer
ö
—library—
_Enter

This works well so far and avoids the insertion of unnecessary layers.
I only have one problem with it - if you want to undo it, you have to undo every single step, it doesn’t behave like a single command, even if it looks like one because of the echo off at the beginning.

Can someone help me here?

best regards

Robbe

Yes, that’s a limitation of macros as far as I know, it’s just like a series of commands being executed one after the other. Scripts undo in one step.

Thanks for the fast reply.

That’s a real bummer.
I had hoped that it could be summarized somehow.

Is there a simple way to convert this into a script to which the address and file name are passed as variables in order to have one script for all parts?

Yes.

Okay no so easy that I’m going to do it right now, but I suspect someone will…

That sounds very promising.
I’ve already tried to get the AI to help me with this.
This is what came out of it:

Option Explicit

Sub LoadPartFromFile(filePath As String, fileName As String)
    ' Turn off command echo
    Call Rhino.Command("_EchoOff", 0)

    ' Import the part
    Call Rhino.Command("-_Import " & filePath & fileName, 0)

    ' Change imported objects to the current layer
    Call Rhino.Command("_ChangeToCurrentLayer", 0)

    ' Set the layer explicitly to avoid creating new layers
    Call Rhino.Command("_-Layer _Name library _Enter", 0)

    ' Turn command echo back on
    Call Rhino.Command("_EchoOn", 0)
End Sub

' Example usage:
' You would call this from Rhino's command line or another script like this:
' Call LoadPartFromFile("R:\Bibliothek\Schrauben\M8_Innensechskant\", "30.3dm")
! _-RunScript "LoadPartFromFile ""R:\Bibliothek\Schrauben\M8_Innensechskant\"" ""30.3dm"""

However, this run command not only contradicts the example in script bottom, but also generates this error message:

Unfortunately, the debugging efforts came to nothing.
Can anyone here help me further, I am still a total novice when it comes to programming and implementation.

best regards

Robbe

After much trial and error, I have now come up with a variant that somehow works.

Here is the call:

! _NoEcho
 _-RunScript "SetPath ""R:\Bibliothek\Schrauben\M8_Innensechskant\30.3dm"""
 _-RunScript LoadPartFromFile

Here is the script:

Option Explicit

' Globale Variable, um den Pfad zu speichern
Dim globalFullPath

Sub SetPath(path)
	globalFullPath = path
End Sub

Sub LoadPartFromFile
    
	' Importieren Sie das Teil
	Rhino.Command "-_Import " & globalFullPath, 0
    
	' Ändern Sie die importierten Objekte auf die aktuelle Ebene
	Rhino.Command "_ChangeToCurrentLayer", 0
    
	' Löschen Sie die Ebene library
	Rhino.Command "_-Layer _Delete ---library--- _Enter", 0
End Sub

Certainly not the most elegant variant, but passing variables directly led to all sorts of dead ends.
The structure of the script also looks a bit weird to me as a layman.

Perhaps someone can give me a hand with this?