Refactor FCStd files

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
Post Reply
leoheck
Veteran
Posts: 1223
Joined: Tue Mar 13, 2018 5:56 pm
Location: Coffee shop

Refactor FCStd files

Post by leoheck »

Hey guys, do we have any tool/script/command to refactor and improve the FCStd files?

I was thinking about refactoring object names and cleaning up any other issues that the file may have.

I have files with a small number of objects but with objects with big numbers being suffixed as in "Origin599"

What I use to do, is to start a new version of an old design by creating a copy of the current version of the file. So, I may have pretty old designs that were being upgraded during months/years and the then object naming is getting crazy.
User avatar
papyblaise
Veteran
Posts: 7870
Joined: Thu Jun 13, 2019 4:28 pm
Location: France

Re: Refactor FCStd files

Post by papyblaise »

I open an existing file, save it under the new name, and continue building on it
I thus have 2 independent files, the second of which is not linked to the first
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Refactor FCStd files

Post by TheMarkster »

I am not aware of any such utility to allow the renaming of objects. I suppose relabeling would be simple enough, but renaming could be a challenge. You'd have to make a new object and copy all the properties over, which means you need to identify the type of object. You would also need to manage any expressions linking to the object because when you delete the old object those expressions will be lost. Perhaps the xml files could be directly modified to rename objects. The FCStd format is just a .zip file that can be extracted like any other. Make your changes to the xml, create a new .zip, rename it to .FCStd. Lots of work, highly error prone, very little real benefit, IMO.

Here is a quick macro to relabel the objects:
relabeler.FCMacro
(706 Bytes) Downloaded 16 times
Use Undo if you don't like the results. (But you should probably backup any important files before trying it, or just don't save them if you don't like the results.)
leoheck
Veteran
Posts: 1223
Joined: Tue Mar 13, 2018 5:56 pm
Location: Coffee shop

Re: Refactor FCStd files

Post by leoheck »

papyblaise wrote: Thu Oct 21, 2021 3:49 pm I open an existing file, save it under the new name, and continue building on it
Yeap, this is kind of the same. I also do this sometimes, actually.


TheMarkster wrote: Thu Oct 21, 2021 4:14 pm Perhaps the xml files could be directly modified to rename objects.
Yeah, I was also thinking that this would be a good approach too.
TheMarkster wrote: Thu Oct 21, 2021 4:14 pm very little real benefit, IMO.
hm... are you sure? I was thinking this would help something. I don't know why, but I have some objects being created with names like "Body033006007016007". Something pretty scary may have happened in the past. So, I am a bit worried if the character count keeps increasing randomly. This object that I have created now has this origin "Origin704" haha

Thanks for the Macro TheMarkster.
leoheck
Veteran
Posts: 1223
Joined: Tue Mar 13, 2018 5:56 pm
Location: Coffee shop

Re: Refactor FCStd files

Post by leoheck »

I could print a list of the object names with your macro.

I removed ones that are pretty random. But these are the current objects. It is kind of crazy. But now I understand which tool may be creating such high numbers. I am using KicadSetpUp tool, to import boards. And they usually have tons of parts.

Code: Select all

 'Binder'
 'Binder001'
 'Body033006007016002'
 'Body033006007016003'
 'Body033006007016004'
 'Body033006007016005'
 'Body033006007016006'
 'Body033006007017'
 'Chamfer'
 'Chamfer001'
 'Chamfer002'
 'Clone'
 'Group'
 'Group001'
 'Group002'
 'Group003'
 'Origin'
 'Origin004'
 'Origin590'
 'Origin591'
 'Origin592'
 'Origin593'
 'Origin594'
 'Origin595'
 'Origin596'
 'Origin597'
 'Origin598'
 'Origin599'
 'Origin601'
 'Origin602'
 'Origin603'
 'Origin604'
 'Origin605'
 'Origin606'
 'Origin607'
 'Origin608'
 'Origin609'
 'Pad'
 'Pad039'
 'Pad040'
 'Pad041'
 'Part'
 'Part010'
 'Part__Feature'
 'Part__Feature003'
 'Part__Feature042025'
 'Part__Feature042026'
 'Part__Feature042027'
 'Part__Feature042028'
 'Part__Feature042029'
 'Part__Feature042030'
 'Part__Feature042031'
 'Part__Feature042032'
 'Part__Feature042033'
 'Part__Feature042034'
 'Part__Feature3803'
 'Part__Feature3804'
 'Part__Feature3805'
 'Part__Feature3806'
 'Part__Feature3807'
 'Part__Feature3808'
 'Part__Feature3809'
 'Part__Feature3810'
 'Part__Feature3811'
 'Part__Feature3812'
 'Part__Feature3813'
 'Part__Feature3814'
 'Part__Feature3815'
 'Part__Feature3816'
 'Part__Feature3817'
 'X_Axis'
 'X_Axis589'
 'X_Axis590'
 'X_Axis591'
 'X_Axis592'
 'X_Axis593'
 'X_Axis594'
 'X_Axis595'
 'X_Axis596'
 'X_Axis598'
 'X_Axis599'
 'X_Axis6...
 'X_Axis600'
 'X_Axis601'
 'X_Axis602'
 'X_Axis603'
 'X_Axis604'
 'X_Axis605'
 'X_Axis606'
 'X_Axis607'
 'X_Axis608'
 'XY_Plane'
 'XY_Plane588'
 'XY_Plane589'
 'XY_Plane590'
 'XY_Plane591'
 'XY_Plane592'
 'XY_Plane593'
 'XY_Plane594'
 'XY_Plane595'
 'XY_Plane596'
 'XY_Plane597'
 'XY_Plane598'
 'XY_Plane600'
 'XY_Plane601'
 'XY_Plane602'
 'XY_Plane603'
 'XY_Plane604'
 'XY_Plane605'
 'XY_Plane606'
 'XY_Plane607'
 'XY_Plane608'
 'XZ_Plane'
 'XZ_Plane588'
 'XZ_Plane589'
 'XZ_Plane590'
 'XZ_Plane591'
 'XZ_Plane592'
 'XZ_Plane593'
 'XZ_Plane594'
 'XZ_Plane595'
 'XZ_Plane596'
 'XZ_Plane597'
 'XZ_Plane598'
 'XZ_Plane600'
 'XZ_Plane601'
 'XZ_Plane602'
 'XZ_Plane603'
 'XZ_Plane604'
 'XZ_Plane605'
 'XZ_Plane606'
 'XZ_Plane607'
 'XZ_Plane608'
 'XZ_Plane609'
 'Y_Axis'
 'Y_Axis566'
 'Y_Axis589'
 'Y_Axis590'
 'Y_Axis591'
 'Y_Axis592'
 'Y_Axis593'
 'Y_Axis594'
 'Y_Axis595'
 'Y_Axis596'
 'Y_Axis598'
 'Y_Axis599'
 'Y_Axis600'
 'Y_Axis601'
 'Y_Axis602'
 'Y_Axis603'
 'Y_Axis604'
 'Y_Axis605'
 'Y_Axis606'
 'Y_Axis607'
 'YZ_Plane'
 'YZ_Plane589'
 'YZ_Plane590'
 'YZ_Plane591'
 'YZ_Plane592'
 'YZ_Plane593'
 'YZ_Plane594'
 'YZ_Plane595'
 'YZ_Plane596'
 'YZ_Plane597'
 'YZ_Plane598'
 'YZ_Plane600'
 'YZ_Plane601'
 'YZ_Plane602'
 'YZ_Plane603'
 'YZ_Plane604'
 'YZ_Plane605'
 'YZ_Plane606'
 'YZ_Plane607'
 'YZ_Plane608'
 'Z_Axis'
 'Z_Axis009'
 'Z_Axis590'
 'Z_Axis591'
 'Z_Axis592'
 'Z_Axis593'
 'Z_Axis594'
 'Z_Axis595'
 'Z_Axis596'
 'Z_Axis597'
 'Z_Axis598'
 'Z_Axis599'
 'Z_Axis601'
 'Z_Axis602'
 'Z_Axis603'
 'Z_Axis604'
 'Z_Axis605'
 'Z_Axis606'
 'Z_Axis607'
 'Z_Axis608'
 'Z_Axis609'
 'Z_Axis610'
 'Z_Axis611'
 
leoheck
Veteran
Posts: 1223
Joined: Tue Mar 13, 2018 5:56 pm
Location: Coffee shop

Re: Refactor FCStd files

Post by leoheck »

Oh, now I realized that your macro is just renaming objects in the object tree. Ah, this is controlled by the user, I don't need this. I was looking for a way to rename the object itself, not this label that is controlled by the user.

Ah, one more thing. The first column is auto-resizing to fit the longest object name. Do you know a way to disable this?
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Refactor FCStd files

Post by TheMarkster »

Here is how you can rename an object.

Let's say it's a sketch.

First, create a new sketch with the name you want:

Code: Select all

sketch = FreeCAD.ActiveDocument.addObject("Sketcher::SketchObject","Sketch")
FreeCAD will use the name you give unless it's already taken, in which case it does something similar to the macro, adding 001, 002, etc. Now, you need to copy all the contents over to the new sketch. Get a reference to the original sketch with something like:

Code: Select all

original_sketch = FreeCAD.ActiveDocument.getObject("Sketch056")
Make a copy in memory:

Code: Select all

copy = original_sketch.dumpContent().hex() #saves it has a hexadecimal string
Copy that over to the new sketch:

Code: Select all

sketch.restoreContent(bytearray.fromhex(copy))
You will need to manually manage any expressions you were using either linking to the sketch or from the sketch, I think. I don't know of a way to do this in a macro. For example, if you duplicate a sketch and another sketch is referencing one of the constraints in the original sketch or the original sketch is referencing a spreadsheet alias you need to parse through the InList and OutList and figure out what's what.

To put this in a complete macro, which I have no intention of doing, you need a way to identify the object type and create a new object based on that type. Probably in most cases the object's TypeId can be used:

Code: Select all

new_obj = FreeCAD.ActiveDocument.addObject(orig_obj.TypeId, stripNumerals(orig_obj.Name)
new_obj.Name gives you the unique name FreeCAD found for this new object.

Then use the copy strategy outlined above. Final step is to remove the original object:

Code: Select all

FreeCAD.ActiveDocument.removeObject(orig_obj.Name)
Post Reply