manipulate points of edges of wire

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
ikua
Posts: 159
Joined: Fri Apr 07, 2017 1:32 pm

manipulate points of edges of wire

Post by ikua »

Hello everyone!

I think this question must have a easy solution, but i can not find it. I want to get with the selection an edge (as part of an wire) an move both vertices for example to the x coordinate 9. I get the connection to the part, and can read the coordinate but i did not find a way to change it. Maybe someone can me give me a small hint. Thanks in forward.

o=Gui.Selection.getSelection()[0]
print(o.TypeId)
print ('hello')
s= o.Shape
print s.Edges
print "Vertices: "
print s.Vertexes[0].Point.x
dir(s.Vertexes[0].Point)
s.Vertexes[0].Point.x=9
print s.Vertexes[0].Point.x

greets ikua
Last edited by ikua on Fri Mar 30, 2018 8:51 pm, edited 1 time in total.
User avatar
Joel_graff
Veteran
Posts: 1949
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: manipulate coordinates of points

Post by Joel_graff »

Here's how I solved that problem:

Code: Select all

def get_selection(sketch_object):
    result = []
    shape_names = Gui.Selection.getSelectionEx()[0].SubElementNames

    for shape_name in shape_names:
        index = int(filter(str.isdigit, shape_name))
        geom = sketch_object.Geometry[index-1]
        result.append(geom)
    
    return result
note: sketch_object refers to the sketch object you get when you reference App.ActiveDocument.Sketch (or whatever it's named)

In order to access the geometry, you have to get the list of selected SubElementNames and strip out the index number at the end of the name. That correlates to the geometry in the sketch object's Geometry list.

Once you get that, you can convert the low-level Coin3D geometry to the Part shapes using:

Code: Select all

geo_list = get_selection(App.ActiveDocument.Sketch)

for geo in geo_list:
    shape = geo.toShape()
    print shape.Vertexes
It's not intuitive. But it works pretty well.

Anyway, that should work. Let me know if it doesn't. Sharing a file helps, too. :)
FreeCAD Trails workbench for transportation engineering: https://www.github.com/joelgraff/freecad.trails

pivy_trackers 2D coin3D library: https://www.github.com/joelgraff/pivy_trackers
ikua
Posts: 159
Joined: Fri Apr 07, 2017 1:32 pm

Re: manipulate coordinates of points

Post by ikua »

Hey Joel!

Thanks for your answer put i get lost in the explanation and I am not able to apply it to my case. Like suggested i added a Sample file.
The left dwire is the starting point. Then i would like to apply a tool, with which i can move on of the edges of the dwire (like in the right dwire the middle edge is lifted 0,5m). Therefore i need to access and manipulate the z-coordinate in this case from the selected edge. Like presented in the post above, i am able to reference everything down to the point. But i do not know how to change it and i am to unexpierenced to get a solution out of your post. Maybe with the sample file it gets clearer what i would need.

Thanks a lot
Attachments
example_dwire_edge.FCStd
(5.21 KiB) Downloaded 56 times
User avatar
Joel_graff
Veteran
Posts: 1949
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: manipulate coordinates of points

Post by Joel_graff »

ikua wrote: Fri Mar 23, 2018 10:51 am i am to unexpierenced to get a solution out of your post
You can't get the solution because I assumed you were working in the Sketcher. My fault for not reading your post more carefully - I admit, though, I haven't gotten much sleep lately. :)

I don't know how to solve this problem, I'm afraid - I haven't had much occasion to work with wires. I know how to move the points in the GUI, but that's it.

Still, I can think of at least one guy who might be able to help.
microelly2 wrote: ping
FreeCAD Trails workbench for transportation engineering: https://www.github.com/joelgraff/freecad.trails

pivy_trackers 2D coin3D library: https://www.github.com/joelgraff/pivy_trackers
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: manipulate coordinates of points

Post by microelly2 »

The fast way:
In the nurbs workbench I have a BSpline/Dwire Editor
in the menu curves the 3rd entry

select your wire
execute the method
you get a table with the points
you can change the coordinates (you see the changes as blue points)
and apply table to write it back



warning: the values are displayed with factor 0.001 because I use it for a geodata project.
bp_745.png
bp_745.png (92.67 KiB) Viewed 4181 times
phpBB [video]
ikua
Posts: 159
Joined: Fri Apr 07, 2017 1:32 pm

Re: manipulate coordinates of points

Post by ikua »

thank you microelly2 for the hint, but i need this for a special workflow. For piping i need to select different edges of different wires to move them all at the same relative distance. Finally i found a solution. The trick is to loop through all points of the wire, manipulate and write them to an array and afterwards assign the whole point-array to the dwire.

Here the code. I would like to make a tool with it, in which i could use an interface like in the draft move tool. Lets see if will get to that point :-)

Code: Select all

import FreeCAD,FreeCADGui,Draft
#when starting macro one ore more edges must be selected (can also be from different dwires)
selAll = FreeCADGui.Selection.getSelectionEx() # " sel " contains the items selected
for sel in selAll:
	#get points of selected edge
	pointsSelected =[]
	for edge in sel.SubObjects:
		pointsSelected.append(edge.Vertexes[0].Point)
		pointsSelected.append(edge.Vertexes[1].Point)	
	points = []
	#Change of coordinates
	xplus =0
	yplus =0
	zplus =-4	
	#create an array with partly new cordinates for the whole wire
	for obj in sel.Object.Points:
		if obj in pointsSelected:#if points part of the selected edge change them
			points.append(FreeCAD.Vector(obj.x+xplus,obj.y+yplus,obj.z+zplus))
		else:
			points.append(FreeCAD.Vector(obj.x,obj.y,obj.z))
	sel.Object.Points = points
FreeCAD.ActiveDocument.recompute() 
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: manipulate points of edges of wire

Post by microelly2 »

I think you are on the right way. If you start a dialog inside the method to configure the movement vector interactive or read it from a Line object it will become comfortable.
ikua
Posts: 159
Joined: Fri Apr 07, 2017 1:32 pm

Re: manipulate points of edges of wire

Post by ikua »

thank you for your motivation.

I would really happy to hack the methode somehow with the draft move interface, but i am not a pro coder and do not know where to start. Maybe a hint?
ikua
Posts: 159
Joined: Fri Apr 07, 2017 1:32 pm

Re: manipulate points of edges of wire

Post by ikua »

Responding to myself, but if somebody else needs it the stretch tool in the draft WB seems to do this job:

https://www.freecadweb.org/wiki/Draft_Stretch
Post Reply