create 2D-polyline trough points with one command
hello!
Comments
-
hello!
is there a possibility to create a 2D-polyline trough points with one command?
with fitpoly i can only create a statistical mean value between the points
This is just an example, in many cases the poly should be set by 100 points or more…
thanks f. helping
0 -
Unordered points is NP-Hard, so you would need to use a solver. Python’s networkx has one that’s easy to use, but a tad slow. Takes about a half second to compute 100 nodes
Google has an extremely fast solver but it’s a bit harder to use
here’s a sample from a CAD perspective
https://www.theswamp.org/index.php?topic=58634
import traceback from pyrx import Ap, Db, Ge, Ed import networkx as nx @Ap.Command("doit") def make_pline_from_unordered_points(): try: db = Db.curDb() ps, ss = Ed.Editor.select([(0, "POINT")]) if ps != Ed.PromptStatus.eOk: raise RuntimeError("Selection Error! {}: ".format(ps)) graph = nx.Graph() positions = [Db.Point(id).position() for id in ss.objectIds()] for i in positions: for j in positions: if i == j: continue graph.add_edge(i, j, weight=i.distanceTo(j)) tsp = list(nx.approximation.traveling_salesman_problem(graph,cycle=False)) pl = Db.Polyline(tsp) plid = db.addToCurrentspace(pl) except Exception as err: traceback.print_exception(err)
0 -
If they are in a XY pattern then can get all the points and just do that sort on X & Y then make a pline. Should be instant.
(defun c:plpts ( / pts ss x)
(defun AHpllst ( lst / x)
(command "_pline")
(while (= (getvar "cmdactive") 1 )
(repeat (setq x (length lst))
(command (nth (setq x (- x 1)) lst))
)
(command "")
)
)(setq ss (ssget '((0 . "POINT"))))
; sorts on 1st two items
(setq pts
(vl-sort pts
'(lambda (a b)
(cond
((< (car a) (car b)))
((= (car a) (car b))
(< (cadr a) (cadr b)))
)
)
)
)
(princ)
)I can see this failing depending on how points are laid out.
0 -
Yeah, just sorting will work some of the time, it gets harder with C or S shapes, TSP solvers aren't perfect either, it’s only an approximation, exact can take years lol
Here’s a wiki on the topic
Google’s is the best; its what maps uses. Also, you can add constraints, like for circuit board design. Here’s the link for PyRx
0 -
Thanks for the suggestions.
I think it wouldn't be too difficult for Bricsys to extend the fitpoly command. With fitpoly, I also need to semi-automatically define the point direction. Then, the resulting polyline (or spline) would only need to pass through the points.0 -
Yes but in what order?
0 -
Try using the FITLINE command.
0 -
@petercad I think I know what you want. Just a quick hack below (wh
ich takes always more trouble than expected) , it eats POINT entities and generates a 3DPoly, it works 3D.
You can use it 2D too and if you want a polyline as output, you can comment and un comment the section at the end by starting lines with or without semicolons.
After selecting a set of points, and a start point, distances are calculated and the closest point is the second vertex of the polyline. Same procedure from second point for remaining points and so on and so on.
So drag file pts-3dpl.lsp in your drawing and test by using command pts-3dpl.
1