Author Topic: -={ Challenge }=- Enclose lines  (Read 5725 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8690
  • AKA Daniel
Re: -={ Challenge }=- Enclose lines
« Reply #15 on: June 30, 2023, 05:43:00 PM »
i used a hull, so it's wrong
I don't see why is wrong, you got to the result at the end, and I'd like to know how to use python with autocad, sounds interesting.

won't work on a "L" shape

zak26

  • Newt
  • Posts: 33
Re: -={ Challenge }=- Enclose lines
« Reply #16 on: June 30, 2023, 05:46:36 PM »
Quote
on the contrary
it is a very complicated task
I wouldn't think of you saying that, you have created a great program that I haven't seen replicated (IRT), And I'd like to ask you how to work with kml files, but that will be on other ocassion.

ScottMC

  • Newt
  • Posts: 191
Re: -={ Challenge }=- Enclose lines
« Reply #17 on: June 30, 2023, 07:57:29 PM »
Certainly a helpful tool zak26 and Lee Mac!
Exciting thing for those tough situations.
Gotta look at this pgm.
Gile put one out that would help simplify..
https://www.theswamp.org/index.php?topic=19865.msg244892#msg244892

zak26

  • Newt
  • Posts: 33
Re: -={ Challenge }=- Enclose lines
« Reply #18 on: June 30, 2023, 08:06:14 PM »
Certainly a helpful tool zak26 and Lee Mac!
Exciting thing for those tough situations.
Gotta look at this pgm.
Gile put one out that would help simplify..
https://www.theswamp.org/index.php?topic=19865.msg244892#msg244892
Didn't know about this one, I used CAB's psimple, which is a good one too.

JohnK

  • Administrator
  • Seagull
  • Posts: 10624
Re: -={ Challenge }=- Enclose lines
« Reply #19 on: June 30, 2023, 09:01:15 PM »
Here was my mockup. It doesn't do any hand-holding (zero error trapping/checking) but it worked in the tests I tossed at it (and the test drawing you supplied).

Sorry it's not very smooth, I was just chipping away at the program here-and-there in between work tasks all day--and I hacked up the pline handling part in 2 minutes after I thought I was already done.

Code - Auto/Visual Lisp: [Select]
  1. (defun pline-enclosure ( / ss
  2.                            ;; support functions
  3.                            gety
  4.                            getx
  5.                            foreachss
  6.                            drawpoly)
  7.   ;; this is a mockup program which draws a polyline around a selection
  8.   ;; set of lines or plines.
  9.   ;;
  10.   ;; NOTE: I did not implement a sort for these points so if the
  11.   ;;       (poly)lines are not processed "in order" this will
  12.   ;;       draw a wobbly outline.
  13.   ;;
  14.   ;; John Kaul - 2023.06.30
  15.  
  16.           (defun gety (ent)
  17.             ;; get association 10 of ent.
  18.             (cdr (assoc 10 (entget ent))))
  19.           (defun getx (ent / ent code)
  20.             ;; get association 11 (or 10 of polylines) of ent.
  21.             ;; NOTE: The handling of polylines is a bit of a hack
  22.             ;;       but works for this mockup.
  23.             (setq ent (entget ent))
  24.             (setq code (cond
  25.                          ((eq (cdr (assoc 0 ent)) "LWPOLYLINE") (setq ent (reverse ent)) 10)
  26.                          (11)))
  27.             (cdr (assoc code ent)))
  28.           (defun foreachss (ss procedure / lst cntr)
  29.             ;; do something to each item in a selection set.
  30.             (setq cntr 0)
  31.             (if (eq (type ss) 'PICKSET)                 ;; if the selection set is a list of items picked
  32.               (repeat (sslength ss)
  33.                       (setq lst                         ;; create a list
  34.                             (append lst
  35.                                     (list
  36.                                       ((eval procedure) (ssname ss cntr)))))
  37.                       (setq cntr (1+ cntr)))
  38.               '*ERROR*
  39.               );_ end if
  40.             lst
  41.             )
  42.           (defun drawpoly (lst)
  43.             ;; draw a poly line from a list of points.
  44.             (entmakex (append (list (cons 0 "LWPOLYLINE")
  45.                                     (cons 100 "AcDbEntity")
  46.                                     (cons 100 "AcDbPolyline")
  47.                                     (cons 90 (length lst))
  48.                                     (cons 70 1))
  49.                               (mapcar '(lambda (p) (cons 10 p)) lst))))
  50.   (setq ss (ssget))                                     ;; get the items to draw around
  51.  
  52.   (drawpoly                                             ;; draw a polyline
  53.     (append
  54.       (foreachss ss 'gety)
  55.       (reverse (foreachss ss 'getx))
  56.       )
  57.     )
  58.   )


EDIT: Fixed code.
« Last Edit: July 01, 2023, 10:54:17 AM by JohnK »
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10624
Re: -={ Challenge }=- Enclose lines
« Reply #20 on: June 30, 2023, 09:04:18 PM »
probably this is a very small task for you guys
on the contrary
it is a very complicated task

wait what!? ...well, I defiantly messed up then (watch, mine probably only works on Tuesdays I bet---every other day it makes toast).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8690
  • AKA Daniel
Re: -={ Challenge }=- Enclose lines
« Reply #21 on: June 30, 2023, 10:04:57 PM »
fixed

Code - Python: [Select]
  1.  
  2. import PyRx as Rx
  3. import PyGe as Ge
  4. import PyGi as Gi
  5. import PyDb as Db
  6. import PyAp as Ap
  7. import PyEd as Ed
  8.  
  9. from ortools.constraint_solver import routing_enums_pb2
  10. from ortools.constraint_solver import pywrapcp
  11.  
  12.  
  13. def PyRxCmd_pydoit():
  14.     try:
  15.         filter = [(Db.DxfCode.kDxfStart, "LINE,LWPOLYLINE")]
  16.         ssres = Ed.Editor.select(filter)
  17.         if ssres[0] != Ed.PromptStatus.eNormal:
  18.             return
  19.    
  20.         points = []
  21.         ss = ssres[1].toList()
  22.         for id in ss:
  23.             if id.isDerivedFrom(Db.Curve.desc()):
  24.                 curve = Db.Curve(id)
  25.                 points.append(curve.getStartPoint())
  26.                 points.append(curve.getEndPoint())
  27.        
  28.         manager = pywrapcp.RoutingIndexManager(len(points) , 1, 0)
  29.         routing = pywrapcp.RoutingModel(manager)
  30.        
  31.         def distance_callback(from_index, to_index):
  32.             from_node = manager.IndexToNode(from_index)
  33.             to_node = manager.IndexToNode(to_index)
  34.             l =  points[from_node]
  35.             r =  points[to_node]
  36.             return int(l.distanceTo(r) * 100)
  37.        
  38.         transit_callback_index = routing.RegisterTransitCallback(distance_callback)
  39.         routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
  40.         search_parameters = pywrapcp.DefaultRoutingSearchParameters()
  41.         search_parameters.first_solution_strategy = (
  42.         routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
  43.         solution = routing.SolveWithParameters(search_parameters)
  44.        
  45.         path = []
  46.         index = routing.Start(0)
  47.         while not routing.IsEnd(index):
  48.             path.append(points[manager.IndexToNode(index)])
  49.             index = solution.Value(routing.NextVar(index))
  50.            
  51.         pathlen = len(path)
  52.         pline = Db.Polyline(pathlen)
  53.  
  54.         for ind in range(0,pathlen):
  55.             p = path[ind]
  56.             pline.addVertexAt(ind, Ge.Point2d(p.x,p.y))
  57.            
  58.         pline.setClosed(True)
  59.         pline.setColorIndex(1)
  60.        
  61.         db = Db.HostApplicationServices().workingDatabase()
  62.         model = Db.BlockTableRecord(db.modelSpaceId(), Db.OpenMode.ForWrite)
  63.         model.appendAcDbEntity(pline)
  64.  
  65.     except Exception as err:
  66.         print(err)
  67.  
  68.  

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8690
  • AKA Daniel
Re: -={ Challenge }=- Enclose lines
« Reply #22 on: June 30, 2023, 10:31:27 PM »
enl doesn't like this case

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8690
  • AKA Daniel
Re: -={ Challenge }=- Enclose lines
« Reply #23 on: June 30, 2023, 10:33:52 PM »
probably this is a very small task for you guys
on the contrary
it is a very complicated task

wait what!? ...well, I defiantly messed up then (watch, mine probably only works on Tuesdays I bet---every other day it makes toast).

Saturday here
Code: [Select]
Command: (PLINE-ENCLOSURE)
Select objects: Specify opposite corner: 62 found
Select objects:
; error: too few arguments

JohnK

  • Administrator
  • Seagull
  • Posts: 10624
Re: -={ Challenge }=- Enclose lines
« Reply #24 on: June 30, 2023, 10:51:46 PM »
Lol smarta55. Too few arguments!? Iíll have to wait until I get the work laptop fired up again on Monday.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

zak26

  • Newt
  • Posts: 33
Re: -={ Challenge }=- Enclose lines
« Reply #25 on: June 30, 2023, 11:16:31 PM »
enl doesn't like this case
I haven't even thought of that case, thanks that'll be something to fix.

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: -={ Challenge }=- Enclose lines
« Reply #26 on: July 01, 2023, 04:49:13 AM »
I wouldn't think of you saying that
I haven't even thought of that case
now we are talking :)

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: -={ Challenge }=- Enclose lines
« Reply #27 on: July 01, 2023, 05:09:48 AM »
wait what!? ...well, I defiantly messed up then (watch, mine probably only works on Tuesdays I bet---every other day it makes toast).
this is the first time i see a program that needs a full week to be tested :)
as for Saturdays - not working

PS
nice disclaimer, though ;)
Quote
  ;; NOTE: I did not implement a sort for these points so if the
  ;;       (poly)lines are not processed "in order" this will
  ;;       draw a wobbly outline.

kasmo

  • Newt
  • Posts: 28
Re: -={ Challenge }=- Enclose lines
« Reply #28 on: July 01, 2023, 07:54:59 AM »
Good enough for the sample dwg, hehe

Code - Auto/Visual Lisp: [Select]
  1. (defun c:outline ( / ss l l2 q p i )
  2.   (setq q (mapcar 'cdr (apply 'append (mapcar '(lambda (y) (vl-remove-if-not '(lambda (x) (member (car x) (list 10 11))) y)) (mapcar 'entget (vl-remove-if 'listp (mapcar 'cadr (ssnamex (ssget)))))))))
  3.   (setq l2 (cons (setq p (last q)) l2))
  4.   (repeat (length q)
  5.     (setq l nil)
  6.     (repeat (setq i (- (length q) 1))
  7.       (setq l (cons (list (distance p (nth (setq i (1- i)) q)) (nth i q)) l))
  8.     )
  9.     (setq l2 (cons (setq p (cadr (car (vl-sort l '(lambda (x y) (< (car x) (car y))))))) l2))
  10.     (setq q (vl-remove p q))
  11.   )
  12.   (entmake (append (list '(0 . "lwpolyline")) (mapcar '(lambda (x) (cons 10 x)) (setq l2 (vl-remove nil l2))) (list (cons 10 (car l2)))))
  13. )
  14.  
« Last Edit: July 01, 2023, 08:18:09 AM by kasmo »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8690
  • AKA Daniel
Re: -={ Challenge }=- Enclose lines
« Reply #29 on: July 01, 2023, 08:07:12 AM »
Good enough for the sample dwg, hehe

are you sure?  :laugh:

Quote
Command: OUTLINE
Select objects: Specify opposite corner: 61 found
Select objects:
; error: no function definition: REMOVE