Macro/Script Assistance

Macro/Script Assistance

Anonymous
Not applicable
1,302 Views
8 Replies
Message 1 of 9

Macro/Script Assistance

Anonymous
Not applicable

I am  really struggling in setting up a macro (if it is even possible) for this particular application.  Within a project I am wanting to rename toolpaths/folders/workplanes etc by replacing part of the name with alternative text.  Basically on the attached image I am wanting to replace anywhere in the project where the is the text "0001_AD" with something else that will be defined by the user with an input box eg. "0003_AD".  The initial text "0001_AD" will be different in every instance, I was thinking that another user input box could be used to define the text that is to be replaced.  Apologies this is not that easy to explain.

 

Basically I am looking for -

- A popup user input box for the user to define the text they are wanting to replace (0001_AD).

- Then anywhere this text is featured in the project whether it be toolpath/folder/tool/workplane it is replaced with another item of text to be defined with a user input box.

 

I have no idea if this in any way possible and was hoping someone out there may be doing something similar already. 

0 Likes
Accepted solutions (2)
1,303 Views
8 Replies
Replies (8)
Message 2 of 9

TK.421
Advisor
Advisor
Accepted solution

you can try the below code.   Copy and paste code for patterns, workplanes, whatever.  this should do what you want, let me know if it doesnt

 

reset localvars

//set text to replace and new text
STRING oldText = INPUT "Enter Text to Search"
STRING newText = INPUT "Enter Replacement Text"

//loop through toolpath folders
STRING LIST tpFolders = get_folders('Toolpath')
FOREACH fld IN tpFolders {
	IF position(fld, oldText) >= 0 {
		STRING nameOnly = replace(fld, "Toolpath\", "")
		STRING newName = replace(nameOnly, oldText, newText)
		RENAME FOLDER $fld  $newName
	}
}

//loop through toolpaths
FOREACH tp IN Folder ('Toolpath') {
	IF position(tp.name, oldText) >= 0 {
		STRING newName = replace(tp.name, oldText, newText)
		RENAME TOOLPATH $tp.name $newName
	}
}

//loop through boundary folders
STRING LIST bnFolders = get_folders('Boundary')
FOREACH fld IN bnFolders {
	IF position(fld, oldText) >= 0 {
		STRING nameOnly = replace(fld, "Boundary\", "")
		STRING newName = replace(nameOnly, oldText, newText)
		RENAME FOLDER $fld  $newName
	}
}

//loop through boundaries
FOREACH bn IN Folder ('Boundary') {
	IF position(bn.name, oldText) >= 0 {
		STRING newName = replace(bn.name, oldText, newText)
		RENAME TOOLPATH $bn.name $newName
	}
}

 

 


the numbers never lie
Message 3 of 9

Anonymous
Not applicable

Works an absolute treat.  Thank you.

0 Likes
Message 4 of 9

Anonymous
Not applicable

ok, so another quick question, am I able to search as above for only folders named the exact search value.  For example, search for "_0001_AD" and only replace the name of folders called this exact name only, as opposed to any folders that have that anywhere in the name eg  it would ignore a folder called "xyz_0001_AD" 

 

I have tried replacing the >= with == and this wasn't the quick fix I was hoping for!

0 Likes
Message 5 of 9

TK.421
Advisor
Advisor

I am sorry it isnt working as expected. Are you sure you copied the code exactly? I ask because I tested it with such a case as you're describing before I posted it. As you can see in the screen shots below, I have it working for me.

 

Could I be misunderstanding you?

Could you post a screen shot of your folders?

Capture.PNG

Capture1.PNGCapture2.PNG

Capture3.PNG

 


the numbers never lie
0 Likes
Message 6 of 9

Anonymous
Not applicable

It is working as I asked in the first post but since then I have tried to move it on slightly.

In a project I may have multiple toolpath folders -

 

"0001_AD"

"0001_AD_xyz"

"0001_AD_dog"

 

I want the macro to search for "0001_AD" and only replace the name of the folder that has that exact same name, thus ignoring the folders "0001_AD_xyz" and "0001_AD_dog".  At present it replaces the "0001_AD" part in any folder name where it sees it (which is what I originally asked for but not what I am trying to do now).

 

Hope that makes sense.

 

Message 7 of 9

TK.421
Advisor
Advisor
Accepted solution

so i did misunderstand your post! try the code below:

 

reset localvars

//set text to replace and new text
STRING oldText = INPUT "Enter Text to Search"
STRING newText = INPUT "Enter Replacement Text"

//loop through toolpath folders
STRING LIST tpFolders = get_folders('Toolpath')
FOREACH fld IN tpFolders {
	STRING nameOnly = replace(fld, "Toolpath\", "")
	IF $nameOnly == $oldText {		
		RENAME FOLDER $fld  $newText
	}
}

the numbers never lie
0 Likes
Message 8 of 9

TK.421
Advisor
Advisor

is this working for you now?


the numbers never lie
0 Likes
Message 9 of 9

Anonymous
Not applicable

Yes it works exactly as I wanted, thanky you, you have been most helpful