Center Selected Sketch Contents

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
duckmug
Posts: 126
Joined: Mon Jun 07, 2021 12:26 am

Center Selected Sketch Contents

Post by duckmug »

I am able to center a solid to the true origin with this macro:

Code: Select all

obj = FreeCADGui.Selection.getSelection()[0]
shp = obj.Shape
center = shp.BoundBox.Center

m = FreeCAD.Matrix()
m.move(center * -1)
movedShape = shp.transformGeometry(m)

new_obj = App.ActiveDocument.addObject('Part::Feature', obj.Name)
new_obj.Shape = movedShape
Is there something similar that I can apply for the selected contents of a sketch?

Using the GUI sketch move tool does not center it to the origin but rather makes it start from it.

It may not seem essential to do this kind of manual centering since a constraint would assist anyway but it would ease a number of cases.
Last edited by duckmug on Tue Jun 15, 2021 11:29 pm, edited 3 times in total.
chrisb
Veteran
Posts: 54273
Joined: Tue Mar 17, 2015 9:14 am

Re: Center Selected Sketch Contents

Post by chrisb »

Watching the forum is an endless source of things to learn, e.g.: https://forum.freecadweb.org/viewtopic. ... 97#p510397 .
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
duckmug
Posts: 126
Joined: Mon Jun 07, 2021 12:26 am

Re: Center Selected Sketch Contents

Post by duckmug »

Indeed. That's neat but it seems to center the camera rather than the selected sketch contents around the origin.

At least that's the effect that I can reproduce here.
chrisb
Veteran
Posts: 54273
Joined: Tue Mar 17, 2015 9:14 am

Re: Center Selected Sketch Contents

Post by chrisb »

Oh sorry, I messed things up, you asked for a sketch. In general this is not possible, you have to add appropriate constraints, e.g. symmetry. Better help is possible if you can give a concrete case.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Syres
Veteran
Posts: 2901
Joined: Thu Aug 09, 2018 11:14 am

Re: Center Selected Sketch Contents

Post by Syres »

duckmug wrote: Tue Jun 15, 2021 10:11 pm Is there something similar that I can apply for the selected contents of a sketch?

Using the GUI sketch move tool does not center it to the origin but rather makes it start from it.
Using @edwilliams16 code as a base, is this what you are looking for (it only centres the entire sketch on the origin not specific geometry):

Code: Select all

import FreeCAD
selt = Gui.Selection.getSelectionEx()
sel = selt[0]
if sel.Object.Shape.ShapeType == 'Compound':
    shift = sel.Object.Shape.SubShapes[0].CenterOfMass
base = App.ActiveDocument.getObject(sel.ObjectName).Placement
base.move(-shift)
App.ActiveDocument.recompute()
duckmug
Posts: 126
Joined: Mon Jun 07, 2021 12:26 am

Re: Center Selected Sketch Contents

Post by duckmug »

Syres wrote: Wed Jun 16, 2021 10:13 am
duckmug wrote: Tue Jun 15, 2021 10:11 pm Is there something similar that I can apply for the selected contents of a sketch?

Using the GUI sketch move tool does not center it to the origin but rather makes it start from it.
Using @edwilliams16 code as a base, is this what you are looking for (it only centres the entire sketch on the origin not specific geometry):

Code: Select all

import FreeCAD
selt = Gui.Selection.getSelectionEx()
sel = selt[0]
if sel.Object.Shape.ShapeType == 'Compound':
    shift = sel.Object.Shape.SubShapes[0].CenterOfMass
base = App.ActiveDocument.getObject(sel.ObjectName).Placement
base.move(-shift)
App.ActiveDocument.recompute()
Hi, thank you for your response.

What I am looking for is for centering sketch contents in sketch edit, not the sketch itself.
chrisb
Veteran
Posts: 54273
Joined: Tue Mar 17, 2015 9:14 am

Re: Center Selected Sketch Contents

Post by chrisb »

duckmug wrote: Wed Jun 16, 2021 10:19 am What I am looking for is for centering sketch contents in sketch edit, not the sketch itself.
View->Standard Views->FitAll or the corresponding icon center the sketch. Sometimes it is a bit surprising because the center is always included.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
drmacro
Veteran
Posts: 9000
Joined: Sun Mar 02, 2014 4:35 pm

Re: Center Selected Sketch Contents

Post by drmacro »

duckmug wrote: Wed Jun 16, 2021 10:19 am What I am looking for is for centering sketch contents in sketch edit, not the sketch itself.
How about when in Sketch edit mode:
https://wiki.freecadweb.org/Sketcher_ViewSketch

Image
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
duckmug
Posts: 126
Joined: Mon Jun 07, 2021 12:26 am

Re: Center Selected Sketch Contents

Post by duckmug »

I meant moving the selected sketch contents so that they are centered around the sketch origin.

As chrisb said though:
In general this is not possible, you have to add appropriate constraints, e.g. symmetry.
duckmug
Posts: 126
Joined: Mon Jun 07, 2021 12:26 am

Re: Center Selected Sketch Contents

Post by duckmug »

I figured that I can solve this by simply moving the shape prior to converting to sketch.
Post Reply