Merge sketches

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
drmacro
Veteran
Posts: 8865
Joined: Sun Mar 02, 2014 4:35 pm

Merge sketches

Post by drmacro »

If I record a macro that merges a selection of sketches I get this:

Code: Select all

# Macro Begin: /home/mac/SharedData/FC_common/Mergesketches.FCMacro +++++++++++++++++++++++++++++++++++++++++++++++++
import FreeCAD
import Sketcher

# Gui.runCommand('Std_DlgMacroRecord',0)
# Gui.Selection.addSelection('Unnamed','Sketch')
# Gui.Selection.addSelection('Unnamed','Sketch001')
# Gui.Selection.addSelection('Unnamed','Sketch002')
# Gui.Selection.addSelection('Unnamed','Sketch003')
# Gui.Selection.addSelection('Unnamed','Sketch004')
# Gui.Selection.addSelection('Unnamed','Sketch005')
# Gui.Selection.addSelection('Unnamed','Sketch006')
# Gui.Selection.addSelection('Unnamed','Sketch007')
# Gui.Selection.addSelection('Unnamed','Sketch008')
# Gui.Selection.addSelection('Unnamed','Sketch009')
# Gui.Selection.addSelection('Unnamed','Sketch010')
# Gui.Selection.addSelection('Unnamed','Sketch011')
### Begin command Sketcher_MergeSketches
App.activeDocument().addObject('Sketcher::SketchObject', 'Sketch012')
App.activeDocument().ActiveObject.Placement = App.activeDocument().Sketch.Placement
App.activeDocument().recompute()
### End command Sketcher_MergeSketches
# Macro End: /home/mac/SharedData/FC_common/Mergesketches.FCMacro +++++++++++++++++++++++++++++++++++++++++++++++++
If I understand what happens the .addObject() adds what is currently in the selection object to the new sketch. Is this correct?

My goal is to merge a set of sketches to one sketch in Python.

Code: Select all

Act_Doc = FreeCAD.ActiveDocument
sketchlist = []
majorcount = 60
majorstep = 5
majordelta = 2.0
for p in range(0,majorcount,majorstep):
    ss=Draft.makeShapeString(String=str(p),FontFile="/usr/share/fonts/truetype/freefont/FreeMono.ttf",Size=3.0,Tracking=0.0)
    plm=FreeCAD.Placement()
    plm.Base=FreeCAD.Vector(majordelta*p, 0.0, 0.0)
    plm.Rotation.Q=(0.0, 0.0, 1.5308084989341915e-17, 1.0)
    ss.Placement=plm
    ss.Support=None
    sketchlist.append(Draft.makeSketch(ss, autoconstraints=True))
    Act_Doc.removeObject(ss.Label)
FreeCAD.ActiveDocument.recompute()
So, once it drops out of the for loop it has a list of sketches. The docs I've been able to google don't imply addObject takes any arguments where I could use the list. It also doesn't appear the selection api addSelection will take a list. (I suppose there might be an append method on the selection object, but, haven't found that...)

Basically I don't know how to translate the FCMacro merge to Python.

The ultimate goal is to create a sketch of the text of a scale (as in ruler or dial). I don't know if there is a way to add ShapeStrings directly to a sketch. But if there is a way that is easier that my feeble attempt, I'd love to hear about it.
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Merge sketches

Post by TheMarkster »

In MeshRemodel I have some code to merge some sketches:

Code: Select all

            sketches=[]
            for obj in self.objs:
                sketches.append(Draft.makeSketch(obj,autoconstraints=True,radiusPrecision=prec))
            doc.recompute()
            FreeCADGui.Selection.clearSelection()
            for sk in sketches:
                if sk:
                    FreeCADGui.Selection.addSelection(sk)
            if len(sketches) >= 2:
                if not "Sketcher_NewSketch" in Gui.listCommands():
                    Gui.activateWorkbench("SketcherWorkbench")
                    Gui.activateWorkbench("MeshRemodelWorkbench")
                FreeCADGui.runCommand("Sketcher_MergeSketches")
Edit: objs is a list of objects the user has selected, presumably draft lines, circles, wires. Possibly a shapestring could also work.
drmacro
Veteran
Posts: 8865
Joined: Sun Mar 02, 2014 4:35 pm

Re: Merge sketches

Post by drmacro »

Thank you!

That got me going and added new stuff to my experience. Had not encountered "FreeCADGui.runCommand()" before. ;)
TheMarkster wrote: Wed Jan 26, 2022 5:02 pm In MeshRemodel I have some code to merge some sketches:

Code: Select all

            sketches=[]
            for obj in self.objs:
                sketches.append(Draft.makeSketch(obj,autoconstraints=True,radiusPrecision=prec))
            doc.recompute()
            FreeCADGui.Selection.clearSelection()
            for sk in sketches:
                if sk:
                    FreeCADGui.Selection.addSelection(sk)
            if len(sketches) >= 2:
                if not "Sketcher_NewSketch" in Gui.listCommands():
                    Gui.activateWorkbench("SketcherWorkbench")
                    Gui.activateWorkbench("MeshRemodelWorkbench")
                FreeCADGui.runCommand("Sketcher_MergeSketches")
Edit: objs is a list of objects the user has selected, presumably draft lines, circles, wires. Possibly a shapestring could also work.
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
Post Reply