Constraint symmetrical point - new control

A forum for research and development of the user interface of FreeCAD
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
last_not_used_login
Posts: 76
Joined: Sun Feb 05, 2023 12:47 am

Re: Constraint symmetrical point - new control

Post by last_not_used_login »

Grub wrote: Wed Apr 05, 2023 5:32 am It should be possible with a macro/script, ask for someone could code this.
Do you know where I can find macro API documentation/ examples? I will try to develop macro for this
Grub
Posts: 306
Joined: Sun Nov 15, 2020 7:28 pm

Re: Constraint symmetrical point - new control

Post by Grub »

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
User avatar
Shalmeneser
Veteran
Posts: 9560
Joined: Wed Dec 23, 2020 12:04 am
Location: Fr

Re: Constraint symmetrical point - new control

Post by Shalmeneser »

Grub wrote: Tue Apr 04, 2023 5:36 am Create the line, create one point on it,
select the line, select the point and click on constraint symetry.
Create the line, create one point on it,
select the line, select the point and click on constraint symmetry.
User avatar
adrianinsaval
Veteran
Posts: 5551
Joined: Thu Apr 05, 2018 5:15 pm

Re: Constraint symmetrical point - new control

Post by adrianinsaval »

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.
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.
chrisb
Veteran
Posts: 54219
Joined: Tue Mar 17, 2015 9:14 am

Re: Constraint symmetrical point - new control

Post by chrisb »

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.
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.
Attachments
Snip macro screenshot-6e8d74.png
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.
Grub
Posts: 306
Joined: Sun Nov 15, 2020 7:28 pm

Re: Constraint symmetrical point - new control

Post by Grub »

chrisb wrote: Which is a not too frequent use case so that I would not create an extra tool for that.
Not asking is this a new feature at this time, but would it be doable with a little macro ?

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...
Grub
Posts: 306
Joined: Sun Nov 15, 2020 7:28 pm

Re: Constraint symmetrical point - new control

Post by Grub »

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!
Try this macro (but it does not work for external geometries at the moment, I will improve this when I have time...):

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()
######################################
You can add this icon
Constraint_PointOnMidPoint.svg
(5.24 KiB) Downloaded 41 times
in a custom toolbar in Sketcher.
last_not_used_login
Posts: 76
Joined: Sun Feb 05, 2023 12:47 am

Re: Constraint symmetrical point - new control

Post by last_not_used_login »

Grub wrote: Fri Apr 07, 2023 9:25 pm
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!
Try this macro (but it does not work for external geometries at the moment, I will improve this when I have time...):

You can add this iconConstraint_PointOnMidPoint.svg in a custom toolbar in Sketcher.
Hi,
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!
Grub
Posts: 306
Joined: Sun Nov 15, 2020 7:28 pm

Re: Constraint symmetrical point - new control

Post by Grub »

I wrote this version which works with external geometries but only works with Realthunder's version

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.
s, = Gui.Selection.getSelectionEx()
subName, = s.SubElementNames
s.Object.getGeometry(subName)
Master branch return :

Code: Select all

AttributeError: 'Sketcher.SketchObject' object has no attribute 'getGeometry'
Realthunder Version return something like:

Code: Select all

<Line segment (24.187,-19.9187,0) (-20.5285,-19.9187,0) >
Why this difference between the Master the RT ?
Post Reply