[solved] get Hole object from selected edge?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

[solved] get Hole object from selected edge?

Post by dprojects »

How to get hole object from the selected edge of the hole. If I select hole edge it refers always to the final Hole object not to one that makes the Hole. So I have to select Sketch and than face. But the problem is if I select the Sketch I don't know what hole it is. So it is not comfortable way to apply dowel to the hole. Maybe there is better way?

Image
Last edited by dprojects on Tue Aug 09, 2022 1:14 pm, edited 1 time in total.

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: get Hole object from selected edge?

Post by TheMarkster »

One way maybe that could work is first make a list of all the Hole objects.

Code: Select all

holes = [o for o in doc.Objects if o.isDerivedFrom("PartDesign::Hole")]
Then where sk is the selected sketch:

Code: Select all

for hole in holes:
    if sk in hole.OutList:
        print ("found it")
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: get Hole object from selected edge?

Post by dprojects »

TheMarkster wrote: Mon Aug 08, 2022 4:31 pm One way maybe that could work is first make a list of all the Hole objects.

Code: Select all

holes = [o for o in doc.Objects if o.isDerivedFrom("PartDesign::Hole")]
Then where sk is the selected sketch:

Code: Select all

for hole in holes:
    if sk in hole.OutList:
        print ("found it")
For select Sketch and surface I have it working. But if I select Sketch I don't see selected hole. Sketch is not visible. So I want to select edge and find the hole object.

I was thinking if there is something like Hole = Edge.Object() ? But probably not?

I think I can make it like that, make list of edges and for each edge find the hole object. This may work. I will try... but first need to improve the hole feature code, clean it little bit, make icons and docs... I hate make docs... Thank you !

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: get Hole object from selected edge?

Post by TheMarkster »

The sketch's Shape attribute will contain a list of edges. Each circle will be one of those edges.

sk.Shape.Edge1.Curve.Center
sk.Shape.Edge1.Curve.Axis

Note: it is possible for a Hole to have multiple holes if the sketch has multiple circles.
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: get Hole object from selected edge?

Post by dprojects »

TheMarkster wrote: Mon Aug 08, 2022 7:27 pm The sketch's Shape attribute will contain a list of edges. Each circle will be one of those edges.

sk.Shape.Edge1.Curve.Center
sk.Shape.Edge1.Curve.Axis

Note: it is possible for a Hole to have multiple holes if the sketch has multiple circles.
I make each hole one by one and search at new object reference. The magicCNC machine simulator make only one hole with one button click so it is not a problem. I didn't know it is possible to make several Sketches, I always had errors about multiple solid, and I wasn't able to create Pad manually if I draw many shapes at one.

The problem with the dowel is that no matter what hole I select it is always Counterbore002, the last hole object that contains all the stuff. If I select Sketch I don't know where the dowel will be created. But good news is that I have finally icons done for the hole tools. So maybe finally I make the docs and than I will try improve the dowels.


Image

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: get Hole object from selected edge?

Post by edwilliams16 »

Following up on @TheMarkster 's suggestion, if we can assume that the sketch is located on the face where the hole is bored, the following will find which hole corresponds to the selected edge. It brute force looks at all the sketch circles and matches their center and axis to the selected one. More work would be required if you have two holes in the exact same location or if the sketch were offset from the face.

Code: Select all


def findHole(sel):
	doc = sel.Document 
	seledge = sel.SubObjects[0]
	center = seledge.Curve.Center
	axis = seledge.Curve.Axis
	#print(center, axis)
	matchSketch = None
	sketches = [o for o in doc.Objects if o.TypeId == "Sketcher::SketchObject"]  
	holes = [o for o in doc.Objects if o.isDerivedFrom("PartDesign::Hole")]
	for sk in sketches:
		edges = sk.Shape.Edges
		for edge in edges:
		    if edge.Curve.TypeId == 'Part::GeomCircle':
		        #print(edge.Curve.Center, edge.Curve.Axis)
		        if edge.Curve.Center.isEqual(center, 1e-7) and (edge.Curve.Axis.isEqual(axis, 1e-7) or edge.Curve.Axis.isEqual(-axis, 1e-7)):
		            matchSketch = sk
	if not matchSketch:
	    App.Console.PrintMessage("Couldn't find sketch\n")
	    return None
	for hole in holes:
	    if matchSketch in hole.OutList:
	        return hole
	else:
	    App.Console.PrintMessage("Couldn't find hole\n")
	    


sel, = Gui.Selection.getSelectionEx()
if sel.SubObjects[0].Curve.TypeId == 'Part::GeomCircle':
    hole = findHole(sel)
    if hole:
        App.Console.PrintMessage(f'Selected edge belongs to {hole.Name}\n')
else:
    App.Console.PrintMessage('Select the rim circle of a hole\n')
Screen Shot 2022-08-08 at 12.43.51 PM.png
Screen Shot 2022-08-08 at 12.43.51 PM.png (75.18 KiB) Viewed 661 times
Attachments
holefind.FCStd
(20.6 KiB) Downloaded 14 times
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: get Hole object from selected edge?

Post by dprojects »

TheMarkster wrote: Mon Aug 08, 2022 7:27 pm sk.Shape.Edge1.Curve.Center
sk.Shape.Edge1.Curve.Axis
I was thinking too much about it. This is simple thing if there is something like edge.Curve. I didn't know there is also edge.Curve.Radius so I can assign the radius directly to the dowel without thinking about the sketch.

I left the sketch2dowel as it was, because this kind of feature might be also useful. I will make new tool to create dowel from selected edge. I already have, but need to figure out if I need to get dowel size? and from where? the wood thickness? the hole depth? and if I have to deal with rotation... but I think I can close this topic as solved. Sorry, and thank you !

Image

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
Post Reply