in which sequence does FreeCAD stores an arc in the Sketcher: anti-clockwise or indifferent or...

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
edwilliams16
Veteran
Posts: 3192
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: in which sequence does FreeCAD stores an arc in the Sketcher: anti-clockwise or indifferent or...

Post by edwilliams16 »

Selecting an arc in sketcher per Carlo:

Code: Select all

>>> sel=Gui.Selection.getSelectionEx()
>>> edge=sel[0].SubObjects[0]
>>> edge
<Edge object at 0x7fcf7103e070>
>>> edge.Vertexes[0].Point
Vector (17.320508075688775, 9.999999999999998, 0.0)
>>> edge.Curve.Center
Vector (8.229637672676257e-28, 0.0, 0.0)
>>> edge.Curve.Radius
20.0
>>> edge.Vertexes[1].Point
Vector (-9.999999999999996, 17.320508075688778, 0.0)
>>> edge.valueAt(edge.FirstParameter)
Vector (17.320508075688775, 9.999999999999998, 0.0)
>>> edge.valueAt(edge.LastParameter)
Vector (-9.999999999999996, 17.320508075688778, 0.0)
Screen Shot 2021-05-27 at 7.47.12 AM.png
Screen Shot 2021-05-27 at 7.47.12 AM.png (11.53 KiB) Viewed 2023 times
JohnB3
Posts: 17
Joined: Sat Feb 06, 2021 3:08 pm

Re: in which sequence does FreeCAD stores an arc in the Sketcher: anti-clockwise or indifferent or...

Post by JohnB3 »

:D fabulous solution and moreover, using what FreeCAD already has built-in. Thank you very much!!
JohnB3
Posts: 17
Joined: Sat Feb 06, 2021 3:08 pm

Re: in which sequence does FreeCAD stores an arc in the Sketcher: anti-clockwise or indifferent or...

Post by JohnB3 »

Thanks to all, I'v something that works. At least one problem left: the spline. In my solution, the spline reveals "x=subelem[ix]
IndexError: tuple index out of range". I cannot find a mechanism to distinguish the spline from a edge. I now, that a spline can be distinguished by the nodes in FreeCAD. But how can I find/get these nodes. Someone can help me a little further, thanks a lot. Below my code (a part of). John.

Code: Select all

from PySide import QtGui

# designation of element: still to implement. 
dsgn=QtGui.QInputDialog.getText(None, "Designation", "Designation of elements by yourself y/n?")[0]
if "y" or "Y" in dsng:
		dsng="yes"
		print("--->your choice: designation by your own")
else: 
		dsng="no"
		print("--->your choice: designation automatically")

# discretization input by user
npoints=QtGui.QInputDialog.getText(None, "Segmented", "number of vertices (default 2):")[0]
try:
	npoints=int(max(2,float(npoints)))
except:
	npoints=2
print("--->your choice for pieces per element: ", npoints)	
npoints=int(max(2,npoints)) 

ix=0
elem=0
print()

def parameters(elem):
	if elem=="ARC":
		print("**this is an**",elem)
	else:
		print("**this is a**",elem)
	#add: export vertices, length, radius, center etc to a .csv file
		# if line then..
		# if circle then...
		# etc.

def handleMouseEvent(eventInfo):
	global ix
	global elem

	if (eventInfo["Button"] == "BUTTON1" and eventInfo["State"])=="UP" :
			
			sel=Gui.Selection.getSelectionEx()
			edge=sel[0]
			subelem=edge.SubElementNames
			x=subelem[ix]
			print("selected item:",ix,"-",x)
									
			if ('Vertex' in x):
				node=edge.PickedPoints
				for x in node:
					print("selected item (x,y,z)",ix,":", x[0],x[1],x[2])
					elem="Point"
				#print("**this is a NODE**")
			elif ('Edge' in x):
				points=edge.SubObjects[ix].discretize(npoints)
				for p in points:
					print("selected item (x,y,z)",ix,":", p.x,p.y,p.z)
				try:
					subobj=edge.SubObjects[ix]
					if hasattr(subobj.Curve, "Center"):
						if subobj.Closed:
							elem="Circle"
							#print("**this is a CIRCLE**")
							# remark: for a circle: first and end last vertex are exactly the same!
						else:
							elem="Arc"
							#print("**this is an ARC**")
					else:
						elem="Line"
						#print("**this is a LINE**")
						
				except: pass	
				
			else:
				print( "UNKNOWN ITEM SELECTED" )
				print("subelem[i]: ",ix,":",x)
			
			parameters(elem)	#module for exporting element variables etc. 
			ix+=1
			print()
			
	elif (eventInfo["Button"] == "BUTTON2" and eventInfo["State"])=="DOWN":
				FreeCADGui.ActiveDocument.ActiveView.removeEventCallback("SoMouseButtonEvent", handleMouseEvent)	
				print()
				print("Right mouse button pressed: selection stopped")
				print(">>>>>FINISHED<<<<<")				

App.Gui.ActiveDocument.ActiveView.addEventCallback("SoMouseButtonEvent", handleMouseEvent)	
User avatar
onekk
Veteran
Posts: 6222
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: in which sequence does FreeCAD stores an arc in the Sketcher: anti-clockwise or indifferent or...

Post by onekk »

It is not clear the question about the spline.
Speaking of a wire you could identify the "segments" that could be straight in other work lines or curve and that curve could be arc or more complex entities.

same is for an edge, so maybe extracting the type of the edge will be returning the "underlying" primitive, "line, arc or spline" but obvious with some real code to speak about will be more easy.

Once you have the type, you could modify it according of his "nature".

Sorry for not being more precise.

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
edwilliams16
Veteran
Posts: 3192
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: in which sequence does FreeCAD stores an arc in the Sketcher: anti-clockwise or indifferent or...

Post by edwilliams16 »

Selecting a single spline in the sketcher

Code: Select all

sel = Gui.Selection.getSelectionEx()
edge = sel[0].SubObjects[0]
properties of edge.Curve appear to define the spline parameters - degree, endpoints etc. It would take a little digging to figure out the details.
JohnB3
Posts: 17
Joined: Sat Feb 06, 2021 3:08 pm

Re: in which sequence does FreeCAD stores an arc in the Sketcher: anti-clockwise or indifferent or...

Post by JohnB3 »

Hi all,

I tried and read a lot, but I didn't succeed to make a workable selection in the Sketcher Editing mode. In the "finished editing mode", there isn't any problem.
After selecting a Point or "a composed element", like an arc, or a closed spline, the selection tuple "Gui.Selection.getSelectionEx()[0].SubObjects gets empty". I found out, that this phenomenon already was described as a non solvablebug by report bug https://tracker.freecadweb.org/view.php?id=1873.
An idea that keeps in my mind, is to lookup the characteristic geometry in the object selection: Gui.Selection.getSelectionEx()[0].Object.Geometry. For this, you should select the center of the arc as "Picked Point" instead of the arc. But you should know the characteristic parameters in advance, as described in the Geometry tuple. So not a real solution.
My goal was to select the geometry as depicted in the sketch, to a file for further execution in a mechanical programme. The sequence of selecting is important for this case. I didn't succeed in a full automation, but for the time being, it is workable for me. Thank you all.
JohnB3
Posts: 17
Joined: Sat Feb 06, 2021 3:08 pm

Re: in which sequence does FreeCAD stores an arc in the Sketcher: anti-clockwise or indifferent or...

Post by JohnB3 »

I also found out, that "Gui.Selection.clearSelection() " clears the selection (of course), but also the index for the selection will get a reset. Hence after having selecting "an empty" tuple, you can reset the selection and continue again with "Gui.Selection.getSelectionEx()" without any problems. The only thing to remark is, that the already selected items are not shown anymore as being already selected. So, there is a chance, that you select twice.
paullee
Veteran
Posts: 5135
Joined: Wed May 04, 2016 3:58 pm

Re: in which sequence does FreeCAD stores an arc in the Sketcher: anti-clockwise or indifferent or...

Post by paullee »

JohnB3 wrote: Tue Jun 15, 2021 6:59 pm I tried and read a lot, but I didn't succeed to make a workable selection in the Sketcher Editing mode. In the "finished editing mode", there isn't any problem.
Not read through the whole thread...

I used SubElementNames earlier in Sketch Edit Mode

Code: Select all

            selection[0].SubElementNames				# Those selected subElements / edges
Then, using Selection Observer, search SelObserver in Code_snippets

Code: Select all

            Gui.ActiveDocument.setEdit(Sketch)
            App.Console.PrintMessage("Select target Edge of the Sketch...  "+ "\n")
            FreeCADGui.Selection.clearSelection()
            s=SelObserver(attributes)
            self.observer = s							
Hope it helps :)
JohnB3
Posts: 17
Joined: Sat Feb 06, 2021 3:08 pm

Re: in which sequence does FreeCAD stores an arc in the Sketcher: anti-clockwise or indifferent or...

Post by JohnB3 »

Little bit off topic, while the topic is about the direction of the arc. However, finally I succeeded in selecting by mouse and got rid of the bug in Freecad which caused the empty tuple after picking a sketch point, by evaluating the distance between a picked point (on the wanted sketch item) and all sketch items of the sketch (App.ActiveDocument.ActiveObject.Geometry). Of course, the wanted item has a very, very small distance. My solution is given in https://forum.freecadweb.org/viewtopic. ... 10#p527124 . Thank you all.
Post Reply