Do you know where I can find macro API documentation/ examples? I will try to develop macro for this
Constraint symmetrical point - new control
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
-
- Posts: 76
- Joined: Sun Feb 05, 2023 12:47 am
Re: Constraint symmetrical point - new control
There is a dedicated section on the forum for this, I hope you will find some help from the developers here :
viewforum.php?f=22
Otherwise there is all the documentation of the wiki :
https://wiki.freecad.org/FreeCAD_Scripting_Basics
viewforum.php?f=22
Otherwise there is all the documentation of the wiki :
https://wiki.freecad.org/FreeCAD_Scripting_Basics
- Shalmeneser
- Veteran
- Posts: 9560
- Joined: Wed Dec 23, 2020 12:04 am
- Location: Fr
Re: Constraint symmetrical point - new control
Create the line, create one point on it,
select the line, select the point and click on constraint symmetry.
- adrianinsaval
- Veteran
- Posts: 5551
- Joined: Thu Apr 05, 2018 5:15 pm
Re: Constraint symmetrical point - new control
Describe a situation where you will use a centerpoint for something else than a coincidence constraint on it. I agree that for shapes other than a line it will be useful but for lines symmetry is usually enough.last_not_used_login wrote: ↑Wed Apr 05, 2023 12:38 am Not exactly - for me a life of cases are for finding the center of line/shape. I need extra point only for geometry reason. So for each time I have to create a new point, select point and line and click symmetric button.
Benefit is just select a line and click icon.
Re: Constraint symmetrical point - new control
You need this whenever the center of the line should be the center of another symmetry. Which is a not too frequent use case so that I would not create an extra tool for that.adrianinsaval wrote: ↑Wed Apr 05, 2023 8:35 pm Describe a situation where you will use a centerpoint for something else than a coincidence constraint on it.
- Attachments
-
- Snip macro screenshot-6e8d74.png (2 KiB) Viewed 1230 times
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Re: Constraint symmetrical point - new control
Not asking is this a new feature at this time, but would it be doable with a little macro ?chrisb wrote: ↑ Which is a not too frequent use case so that I would not create an extra tool for that.
I think last_not_used_login (and me too and maybe other users) would just create a custom icon and use a simple macro.
I'm trying on my side to make a small macro but I'm stuck on simple things : how to select only "line"? but in sketcher all elements are edges (circle, line, spline...)
How to create a point near the center of line ? how to get the coordinates of the ends of line selected ? etc
So a whole bunch of questions that a simple Freecad user can't answer, you have to know how to code in python to write a simple macro...
I'll ask for help on the forum if I can't make progress...
Re: Constraint symmetrical point - new control
Try this macro (but it does not work for external geometries at the moment, I will improve this when I have time...):last_not_used_login wrote: ↑Mon Apr 03, 2023 11:04 pm Hi,
Many of times I have to create symmetrical point on the line/ between two points - mostly for construction geometry.
For example to create center point for square I need to mark two points on one side and click symmetry, do the same for other side.
Would be great if you could add something like "Add constraint symmetrical point" which creates symmetrical points with constraint between two point. Line can be selected as well.
Thanks!
Code: Select all
###############################################################
#Macro to create a point constraint in middle of a selected line
#
#Select a line and run the macro
#
###############################################################
from PySide import QtGui
if (Gui.Selection.getSelectionEx() == []): # selection is void ?
print('Select a line')
QtGui.QMessageBox.information(None,"",'Select a line')
else :
s, = Gui.Selection.getSelectionEx() # get the selection
if ((len(s.SubElementNames) != 1) or (s.SubElementNames[0][0:4] != 'Edge')): # select only one line or Edge ?
print('Select only one \"Line\"')
QtGui.QMessageBox.information(None,"",'Select only one \"Line\"')
else:
indexElement = int(s.SubElementNames[0][4:]) -1 # index of selected element; TODO : find better way !
sketchName = s.ObjectName # object name
sketch= getattr(App.ActiveDocument, sketchName)
typeElement = sketch.Geometry[indexElement].TypeId # type of element
if ( typeElement != 'Part::GeomLineSegment'): # only 'LineSegment' ? (reject Circle and other...)
print('Select only \"Line\" element')
QtGui.QMessageBox.information(None,"",'Select only \"Line\" element')
else :
line = s.Object.Geometry[indexElement]
middle_point = line.toShape().CenterOfMass
mPoint = App.getDocument(s.DocumentName).getObject(sketchName).addGeometry(Part.Point(App.Vector(middle_point))) #create point on middle of line
App.getDocument(s.DocumentName).getObject(sketchName).addConstraint(Sketcher.Constraint('Symmetric',indexElement,1,indexElement,2,mPoint,1)) # symmetric constraint
App.ActiveDocument.recompute()
Gui.Selection.clearSelection()
######################################
-
- Posts: 76
- Joined: Sun Feb 05, 2023 12:47 am
Re: Constraint symmetrical point - new control
Hi,Grub wrote: ↑Fri Apr 07, 2023 9:25 pmTry this macro (but it does not work for external geometries at the moment, I will improve this when I have time...):last_not_used_login wrote: ↑Mon Apr 03, 2023 11:04 pm Hi,
Many of times I have to create symmetrical point on the line/ between two points - mostly for construction geometry.
For example to create center point for square I need to mark two points on one side and click symmetry, do the same for other side.
Would be great if you could add something like "Add constraint symmetrical point" which creates symmetrical points with constraint between two point. Line can be selected as well.
Thanks!
You can add this iconConstraint_PointOnMidPoint.svg in a custom toolbar in Sketcher.
I will try, thanks. If I find how to select external geometry I try to extend. Just need to find time to read docs
Thanks!
Re: Constraint symmetrical point - new control
I wrote this version which works with external geometries but only works with Realthunder's version
It doesn't work with Master Branch because it seems that the propertie 'getGeometry' is not?/no longer? implemented in the Master branch, I haven't found another way so far.
Realthunder Version return something like:
Why this difference between the Master the RT ?
Code: Select all
###############################################################
#Macro to create a point constraint in middle of a selected line
#internal or external geometry
#Select a line and run the macro
#
###############################################################
from PySide import QtGui
def pointOnMiddle():
sketchName = s.ObjectName # object name
sketch= s.Object
middle_point = geom.toShape().CenterOfMass
mPoint = App.getDocument(s.DocumentName).getObject(sketchName).addGeometry(Part.Point(App.Vector(middle_point))) #create point on middle of line
App.getDocument(s.DocumentName).getObject(sketchName).addConstraint(Sketcher.Constraint('Symmetric',indexElement,1,indexElement,2,mPoint,1)) # symmetric constraint
App.ActiveDocument.recompute()
if (Gui.Selection.getSelectionEx() != []): # selection not void ?
s, = Gui.Selection.getSelectionEx() # get the selection
if (len(s.SubElementNames) != 1): # select only one line or Edge ?
print('02 Sélectionner seulement une \"Ligne\"')
QtGui.QMessageBox.information(None,"",'02 Sélectionner seulement une \"Ligne\"')
else :
subName, = s.SubElementNames
try :
geom = s.Object.getGeometry(subName) # "getGeometry" fonctionne uniquement avec la version RT !
if ( str(geom)[1:5] != 'Line'): # only 'Line' ? (reject Circle and other...)
print('03 Sélectionner seulement un élément de type \"Ligne\"')
QtGui.QMessageBox.information(None,"",'03 Sélectionner seulement un élément de type \"Ligne\"')
else :
if (subName[0:4] == 'Edge') :
indexElement = int(subName[4:]) -1 # index of selected element; TODO : find better way !
pointOnMiddle()
elif (subName[0:12] == 'ExternalEdge') :
indexElement = -3 - (int(subName[12:]) -1) # index external geometry start at -3 !
pointOnMiddle()
else :
print('04 Sélectionner seulement une \"Ligne\"')
QtGui.QMessageBox.information(None,"",'04 Sélectionner seulement une \"Ligne\"')
except :
print('Sélection invalide')
QtGui.QMessageBox.information(None,"",'04 Sélection invalide')
else :
print('01 Sélectionner une ligne')
QtGui.QMessageBox.information(None,"",'01 Sélectionner une ligne')
Gui.Selection.clearSelection()
######################################
It doesn't work with Master Branch because it seems that the propertie 'getGeometry' is not?/no longer? implemented in the Master branch, I haven't found another way so far.
Master branch return :s, = Gui.Selection.getSelectionEx()
subName, = s.SubElementNames
s.Object.getGeometry(subName)
Code: Select all
AttributeError: 'Sketcher.SketchObject' object has no attribute 'getGeometry'
Code: Select all
<Line segment (24.187,-19.9187,0) (-20.5285,-19.9187,0) >