Fast way to import external face geometry into sketch?

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!
TheJust
Posts: 18
Joined: Wed Dec 30, 2020 6:21 pm

Fast way to import external face geometry into sketch?

Post by TheJust »

my workflow:

1. create shape
2. shapebinder faces i want to unfold
3. convert shapebinder to mesh
4. unfold mesh
5. padd unfolded mesh to get a part (1mm)
6. Create sketch on unfolded part
7. Import all the edges of the unfolded part <-- this is REALLY slow because you can only import one line at a time. is there a faster way to do this?
8. Add extra geometry for foam cutting process.
9. Export sketch to SVG
10. layout plans for cutting.

I have tried doing draft -> sketch but when you do that, it all comes in unconstrained and when you start adding extra geometry everything goes sideways. Importing external geometry ensures that everything stays where it should be.

This workflow works, but wow is it slow. All i really need is "import external geometry with chaining" so all lines connected to the imported line also get imported.

anyway, I've attached the file if anyone has a suggestion for how to improve my workflow.

Thanks,
Attachments
FoamBoardFloats.FCStd
(107.74 KiB) Downloaded 37 times
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Fast way to import external face geometry into sketch?

Post by TheMarkster »

This macro might help you.

Code: Select all

# -*- coding: utf-8 -*-
# sketcher_add_subobjects_as_external.FCMacro, 2021 by <TheMarkster>
# add as external links to selected sketch all selected subobjects if subojects selected
# else add all edges of selected object to selected sketch
# if 2 or more sketches are selected the first selected gets the external links added to it
# using visible edges (non external and non constructive) from 2nd sketch and any other additional objects selected

sketch = None
subobjects = []

def addSubs(selobj,subs):
    if not selobj.HasSubObjects:
        #assumption: user wants to add all edges of selected object to the sketch
        ii = 1
        for edge in selobj.Object.Shape.Edges:
            subs.append((selobj.Object.Name,"Edge"+str(ii)))
            ii += 1
    else: #user has selected some subobjects, so we will add all selected edges and vertices (but not faces)
        for sen in selobj.SubElementNames:
            if "Edge" in sen or "Vertex" in sen:
                subs.append((selobj.Object.Name,sen))

sel = FreeCADGui.Selection.getSelectionEx()
for s in sel:
    if s.Object.TypeId == "Sketcher::SketchObject":
        if sketch:
            addSubs(s,subobjects) #assumption: user wants to add edges from 2nd sketch as external links in first sketch
        else:
            sketch = s.Object
            continue
    else:
        addSubs(s,subobjects)

if sketch and len(subobjects) != 0:
    for sub in subobjects:
        FreeCAD.Console.PrintMessage("Adding "+sub[0]+"."+sub[1]+" as external geometry to "+sketch.Label)
        try:
            sketch.addExternal(sub[0], sub[1])
            FreeCAD.Console.PrintMessage("...success\n")
        except:
            FreeCAD.Console.PrintError("...failure\n")
        
elif sketch:
    FreeCAD.Console.PrintError("No subobjects to add.\n")
else:
    FreeCAD.Console.PrintError("No sketch selected.\n")
User avatar
thomas-neemann
Veteran
Posts: 11729
Joined: Wed Jan 22, 2020 6:03 pm
Location: Osnabrück DE 🇩🇪
Contact:

Re: Fast way to import external face geometry into sketch?

Post by thomas-neemann »

TheJust wrote: Tue Dec 07, 2021 12:27 am
it could be that "defining edge" from the realthunder version does your job quickly and automatically. seen here

https://www.youtube.com/watch?v=oQklf1mP_j8

phpBB [video]
Gruß Dipl.-Ing. (FH) Thomas Neemann

https://www.youtube.com/@thomasneemann5 ... ry=freecad
TheJust
Posts: 18
Joined: Wed Dec 30, 2020 6:21 pm

Re: Fast way to import external face geometry into sketch?

Post by TheJust »

Thanks, reading the macro this seems to do exactly what I wanted which would be "import external edge - chained select"

I'll give it a try tonight.
TheMarkster wrote: Tue Dec 07, 2021 2:41 am This macro might help you.
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Fast way to import external face geometry into sketch?

Post by TheMarkster »

I have revised the macro slightly to allow for faces to also be selected and added. I'm not sure the sketcher really works well with faces, but it does work. When I tried it on a cube the external lines extended far beyond the boundary of the selected faces. Edge selection will probably work better, but will also be more tedious.

Code: Select all

# -*- coding: utf-8 -*-
# sketcher_add_subobjects_as_external.FCMacro, 2021 by <TheMarkster>
# add as external links to selected sketch all selected subobjects if subojects selected
# else add all edges of selected object to selected sketch
# if 2 or more sketches are selected the first selected gets the external links added to it
# using visible edges (non external and non constructive) from 2nd sketch and any other additional objects selected

sketch = None
subobjects = []

def addSubs(selobj,subs):
    if not selobj.HasSubObjects:
        #assumption: user wants to add all edges of selected object to the sketch
        ii = 1
        for edge in selobj.Object.Shape.Edges:
            subs.append((selobj.Object.Name,"Edge"+str(ii)))
            ii += 1
    else: #user has selected some subobjects, so we will add all selected edges and vertices and faces
        for sen in selobj.SubElementNames:
            subs.append((selobj.Object.Name,sen))

sel = FreeCADGui.Selection.getSelectionEx()
for s in sel:
    if s.Object.TypeId == "Sketcher::SketchObject":
        if sketch:
            addSubs(s,subobjects) #assumption: user wants to add edges from 2nd sketch as external links in first sketch
        else:
            sketch = s.Object
            continue
    else:
        addSubs(s,subobjects)

if sketch and len(subobjects) != 0:
    for sub in subobjects:
        FreeCAD.Console.PrintMessage("Adding "+sub[0]+"."+sub[1]+" as external geometry to "+sketch.Label)
        try:
            sketch.addExternal(sub[0], sub[1])
            FreeCAD.Console.PrintMessage("...success\n")
        except:
            FreeCAD.Console.PrintError("...failure\n")
        
elif sketch:
    FreeCAD.Console.PrintError("No subobjects to add.\n")
else:
    FreeCAD.Console.PrintError("No sketch selected.\n")
TheJust
Posts: 18
Joined: Wed Dec 30, 2020 6:21 pm

Re: Fast way to import external face geometry into sketch?

Post by TheJust »

Okay i finally got a chance to try this. but I can't seem to get it to do anything. What would be the correct order of operations to use it? I can't select a face (or an edge) and select the sketch at the same time. Could you give a quick how to?
TheMarkster wrote: Tue Dec 07, 2021 6:31 pm
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Fast way to import external face geometry into sketch?

Post by TheMarkster »

Hold down Ctrl key to multiple select. Select the edges/faces/vertices in the 3d view and the sketch in the tree view.
TheJust
Posts: 18
Joined: Wed Dec 30, 2020 6:21 pm

Re: Fast way to import external face geometry into sketch?

Post by TheJust »

Once again, thank you for the help. But for the life of me I cannot get the script to work. I'm getting:

"import_Chained.FCMacro", line 40, in <module>
sketch.addExternal(sub[0],sub[1])
<class 'ValueError'>: Expect either document object or tuple(obj, subname)"

I'm running LinkStage3 if that could make a difference.

Thanks.
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Fast way to import external face geometry into sketch?

Post by TheMarkster »

It might not be compatible for some reason with linkstage3. I have revised it. You can try and see if it works.


Code: Select all

# -*- coding: utf-8 -*-
# sketcher_add_subobjects_as_external.FCMacro, 2021 by <TheMarkster>
# add as external links to selected sketch all selected subobjects if subojects selected
# else add all edges of selected object to selected sketch
# if 2 or more sketches are selected the first selected gets the external links added to it
# using visible edges (non external and non constructive) from 2nd sketch and any other additional objects selected

sketch = None
subobjects = []

def addSubs(selobj,subs):
    if not selobj.HasSubObjects:
        #assumption: user wants to add all edges of selected object to the sketch
        ii = 1
        for edge in selobj.Object.Shape.Edges:
            subs.append((selobj.Object,"Edge"+str(ii)))
            ii += 1
    else: #user has selected some subobjects, so we will add all selected edges and vertices and faces
        for sen in selobj.SubElementNames:
            subs.append((selobj.Object,sen))

sel = FreeCADGui.Selection.getSelectionEx()
for s in sel:
    if s.Object.TypeId == "Sketcher::SketchObject":
        if sketch:
            addSubs(s,subobjects) #assumption: user wants to add edges from 2nd sketch as external links in first sketch
        else:
            sketch = s.Object
            continue
    else:
        addSubs(s,subobjects)

if sketch and len(subobjects) != 0:
    for sub in subobjects:
        FreeCAD.Console.PrintMessage("Adding "+sub[0].Name+"."+sub[1]+" as external geometry to "+sketch.Label)
        try:
            sketch.addExternal(sub[0], sub[1])
            FreeCAD.Console.PrintMessage("...success\n")
        except:
            try:
                sketch.addExternal(sub[0].Name, sub[1])
                FreeCAD.Console.PrintMessage("...success\n")
            except:
                FreeCAD.Console.PrintError("...failure\n")
        
elif sketch:
    FreeCAD.Console.PrintError("No subobjects to add.\n")
else:
    FreeCAD.Console.PrintError("No sketch selected.\n")
TheJust
Posts: 18
Joined: Wed Dec 30, 2020 6:21 pm

Re: Fast way to import external face geometry into sketch?

Post by TheJust »

Still fails. error below. I'm chosing just one edge from Extrude001 along the top face of the extrude and trying to import that into Sketch 002.

Again thank you for the assistance.

Code: Select all

# -*- coding: utf-8 -*-
20:22:47  Adding Extrude001.Edge1251 as external geometry to Sketch002<Sketch> Import_Chained3.FCMacro(37)|SketchObject.cpp(7433): Unknown type of geometry in FoamBoardFloats#Sketch002: Extrude001.
20:22:47  ...failure
Post Reply