A change in file location; how to handle this?

Discussion about the development of the Assembly workbench.
kbwbe
Veteran
Posts: 1049
Joined: Tue Apr 10, 2018 3:12 pm
Location: Germany, near Köln (Cologne)

Re: A change in file location; how to handle this?

Post by kbwbe »

Koemi wrote:
Hi Koemi,
here a small script with which you can replace entries of property "sourceFile" within A2 files.

Code: Select all

# -*- coding: utf-8 -*-

import FreeCADGui,FreeCAD
from PySide import QtGui, QtCore
import os, copy, time, sys, platform


OLD_CAD_FOLDER = "Z:\\blabla\\FreeCAD_files\\"
NEW_CAD_FOLDER = "D:\\raldiedal\\etc\\FreeCAD_files\\"

doc = FreeCAD.activeDocument()

count = 0
for obj in doc.Objects:
    if hasattr(obj, 'sourceFile'):
        if obj.sourceFile.startswith(OLD_CAD_FOLDER):
            obj.sourceFile = obj.sourceFile.replace(OLD_CAD_FOLDER,NEW_CAD_FOLDER)
            count += 1
print (
    "{} names of sourcefile have been replaced\n".format(
        count
        )
    )
.
1) Copy the macro to your macro folder
2) edit the macro and set your desired values of OLD_CAD_FOLDER and NEW_CAD_FOLDER
3) Save one of your bigger assemblies somewhere for testing
4) run the makro on this file being opened
5) please check, whether all sourceFile-names of imported parts are correctly set.

This test works only for one opened assembly, not for the subassemblies. You have to run this script again there.
What estimated number of assemblies do you have ?

Hint: you need \\ in path to get \
Attachments
asm_replace_sourcefiles.py
(583 Bytes) Downloaded 26 times
KBWBE

https://github.com/kbwbe/A2plus
latest release: v0.4.56, installable via FreeCAD's addon manager
Tutorial: gripper assembly https://www.youtube.com/watch?v=QMxcQ5tssWk
Documentation: https://www.freecadweb.org/wiki/A2plus_Workbench
Koemi
Posts: 142
Joined: Thu Dec 28, 2017 11:13 am
Location: The Netherlands

Re: A change in file location; how to handle this?

Post by Koemi »

Great! I hope to try your macro it tomorrow! :D
kbwbe wrote: Thu Dec 20, 2018 10:50 am ...
2) edit the macro and set your desired values of OLD_CAD_FOLDER and NEW_CAD_FOLDER
...
Input the "OLD_CAD_FOLDER" is necessary? I ask this because the assembly file needs to be opened also...?
kbwbe wrote: Thu Dec 20, 2018 10:50 am ...
What estimated number of assemblies do you have ?

...
For some reason W10 explorer search is disfunctional... :cry:

I guess about 50 assemblies... :?
kbwbe
Veteran
Posts: 1049
Joined: Tue Apr 10, 2018 3:12 pm
Location: Germany, near Köln (Cologne)

Re: A change in file location; how to handle this?

Post by kbwbe »

Koemi wrote: Thu Dec 20, 2018 3:07 pm Input the "OLD_CAD_FOLDER" is necessary? I ask this because the assembly file needs to be opened also...?
OLD_CAD_FOLDER is needed. It informs the script, which part of the path (up to which Slash) will be cut away and replaced the NEW_CAD_FOLDER value.
These two values should only contain the beginning of the path that is changing due to new disk drive. So your subfolder structure is still referenced correctly.

Example:
OLD_CAD_FOLDER = "C:\\users\\koemi\\CAD_OLD\\"
NEW_CAD_FOLDER = "Z:\\CAD_NEW\\"

The script is working case SENSITIVE. Type old cad folder with big and small letters exactly as stored name in FC.

Please do a test with one single file first and have a look at the created pathes.
If test succeeds:
- Make sure you have backups.
- Copy your whole CAD folder to the new disk/place
- Open each assembly/subassembly one by one at the new place and run the script. Test access to imported parts.

If something goes wrong, you still have your old folder with the files and can try again.
KBWBE

https://github.com/kbwbe/A2plus
latest release: v0.4.56, installable via FreeCAD's addon manager
Tutorial: gripper assembly https://www.youtube.com/watch?v=QMxcQ5tssWk
Documentation: https://www.freecadweb.org/wiki/A2plus_Workbench
Koemi
Posts: 142
Joined: Thu Dec 28, 2017 11:13 am
Location: The Netherlands

Re: A change in file location; how to handle this?

Post by Koemi »

It does not work... :?:

To test I did the following in advance:
1. Created a brandnew assembly
2. Created a brandnew sub-assembly
3. Added several parts inside the assembly (from different locations)
4. Added the sub-assembly inside the assembly
5. Copied the assembly to: C:\Users\koemi\FreeCAD\Copytest\

Edited the macro:
OLD_CAD_FOLDER = "Z:\\server\\whatever\\R&D\\Engineering\\FreeCAD\\Testfolder\\"
NEW_CAD_FOLDER = "C:\\Users\\koemi\\FreeCAD\\Copytest\\"

Saved the macro (CTRL+S)

Testing:
6. Opened the original assembly from it's original location and RUN script >>> nothing
7. Opened the original assembly from it's copied location and RUN script >>> nothing

Then I tried all the same (from 5.) with the sub-assembly >>> nothing

When I activate the macro I do not get any message like "...names of sourcefile have been replaced...".
kbwbe
Veteran
Posts: 1049
Joined: Tue Apr 10, 2018 3:12 pm
Location: Germany, near Köln (Cologne)

Re: A change in file location; how to handle this?

Post by kbwbe »

Koemi wrote: Fri Dec 21, 2018 8:59 am
When I activate the macro I do not get any message like "...names of sourcefile have been replaced...".
Hi Koemi,
do you have error messages in the report view ?
Also the message should appear there.
KBWBE

https://github.com/kbwbe/A2plus
latest release: v0.4.56, installable via FreeCAD's addon manager
Tutorial: gripper assembly https://www.youtube.com/watch?v=QMxcQ5tssWk
Documentation: https://www.freecadweb.org/wiki/A2plus_Workbench
Koemi
Posts: 142
Joined: Thu Dec 28, 2017 11:13 am
Location: The Netherlands

Re: A change in file location; how to handle this?

Post by Koemi »

Hi kbwbe

Whatever I try:

Report view says: "0 names of sourcefile have been replaced"...
triplus
Veteran
Posts: 9475
Joined: Mon Dec 12, 2011 4:45 pm

Re: A change in file location; how to handle this?

Post by triplus »

Note that Assembly 2 does support relative paths, and for that no scripts are needed. When starting an assembly project, you need to move the part files in the same assembly directory. You can freely move that folder around after, if desired.

First the absolute path is checked, and if the file does not exist, relative path is checked.
kbwbe
Veteran
Posts: 1049
Joined: Tue Apr 10, 2018 3:12 pm
Location: Germany, near Köln (Cologne)

Re: A change in file location; how to handle this?

Post by kbwbe »

Hi @Koemi,
is @Triplus' proposal practicable for you ?
KBWBE

https://github.com/kbwbe/A2plus
latest release: v0.4.56, installable via FreeCAD's addon manager
Tutorial: gripper assembly https://www.youtube.com/watch?v=QMxcQ5tssWk
Documentation: https://www.freecadweb.org/wiki/A2plus_Workbench
Koemi
Posts: 142
Joined: Thu Dec 28, 2017 11:13 am
Location: The Netherlands

Re: A change in file location; how to handle this?

Post by Koemi »

kbwbe wrote: Fri Dec 21, 2018 11:28 am Hi @Koemi,
is @Triplus' proposal practicable for you ?
Koemi
Posts: 142
Joined: Thu Dec 28, 2017 11:13 am
Location: The Netherlands

Re: A change in file location; how to handle this?

Post by Koemi »

kbwbe wrote: Fri Dec 21, 2018 11:28 am Hi @Koemi,
is @Triplus' proposal practicable for you ?
If I understand triplus correctly: no. It would be quite a hassle...

Because my assemblies can contain parts from several other folders, like: tools, hinges, profiles, electronics, logistics, etc. These folders are in fact libraries of parts. I am quite sure I am not the only one who works like this.

To create what triplus says, I have to put all parts in the same folder as the main assembly (that would be quite a job). The 'Pack & Go' function of SolidWorks does exactly the same in a single click. It 'grabs' everything necessary to build the assembly and makes a package of it. But connections of the library-folders are gone. So it is a fast but not perfect solution.
Post Reply