TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: highflyingbird on May 27, 2012, 09:53:28 PM

Title: ==={Challenge}===Find the ridge lines of sloped roof
Post by: highflyingbird on May 27, 2012, 09:53:28 PM
Given a closed polyline, To find the ridge lines of sloped roof, suppose if  every side  has the same slope.

 
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: Serge J. Gianolla on May 27, 2012, 10:54:25 PM
Where were you 6 years ago!  8-)
http://www.theswamp.org/index.php?topic=9659.msg123862;topicseen#msg123862
 ;-)

Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: Lee Mac on May 28, 2012, 06:42:50 AM
I have a LISP solution which I believe yields the correct straight skeleton result for 90% of cases, but have not released it as freeware:

(http://www.theswamp.org/lilly_pond/leemac/Straight-Skeleton.gif)

Some more examples:

(http://www.theswamp.org/lilly_pond/leemac/Straight-Skeleton2.gif)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: highflyingbird on May 28, 2012, 07:00:48 AM
I have a LISP solution which I believe yields the correct straight skeleton result for 90% of cases, but have not released it as freeware:
..
You are great! I wish it won't undercut your business! :lmao:
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: Krushert on May 28, 2012, 07:41:47 AM
Not to be a party pooper.  But I only about the 15% buildings I do have a consistant pitch thus having the ridge line at the same height. 

My architects like to have a roof have the following artistic charater and charming features:
:pissed:
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: CAB on May 28, 2012, 09:00:44 AM
Where were you 6 years ago!  8-)
http://www.theswamp.org/index.php?topic=9659.msg123862;topicseen#msg123862
 ;-)
Yes, that one has alluded me for a long time.  :-(
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: CAB on May 28, 2012, 09:15:14 AM
Current project. Roof ridge is not correct but close.
It is a remodel so I don't need it to be correct.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: Krushert on May 28, 2012, 09:39:10 AM
Current project. Roof ridge is not correct but close.
It is a remodel so I don't need it to be correct.
:-o Holly Batman and I thought I had it bad!  We do a lot of wood framed apartment buildings so little more consistency before a change in a roof plane.  I forgot about McMansions.

Do you usually the document the plate height on the roof plan?  I kind a like that.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: CAB on May 28, 2012, 10:38:28 AM
Plate height usually appear on the framing plan in the form of different hatches on the walls (plan view).
If it gets too complicated I provide a separate sheet just for plate heights.
The roof plan as you see above usual ends up as a 1/8 scale on the framing plan if there is room.
So at 1/8 scale it might be tough to see the text but I like that idea.  8-)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on May 31, 2012, 12:04:22 PM
Here is mine version (nobody posted code :( ) and as I am not real programmer as Lee, it's quite risky to be used... With some complex plines it can make quite mess, but on the other hand maybe someone can use it as it makes construction of roof solution... I just don't know how to make it better and as Lee won't post his solution at least I tried...

Code - Auto/Visual Lisp: [Select]
  1. (defun pll (pl / K MSP PLA PLAO PLO PLOPTLST PLPTLST PTT PTTLST PTTLSTN DST VAR) (vl-load-com)
  2.   (setq pla (vlax-ename->vla-object pl))
  3.   (if (vl-catch-all-error-p (setq var (vl-catch-all-apply 'vla-offset (list pla -1e-3)))) (setq plao nil) (setq plao (car (vlax-safearray->list (vlax-variant-value var)))))
  4.   (if plao (setq plo (vlax-vla-object->ename plao)))
  5.   (if (> (cdr (assoc 90 (entget pl))) 2)
  6.     (progn
  7.       (mapcar '(lambda (x) (if (eq (car x) 10) (setq plptlst (cons (cdr x) plptlst)))) (entget pl))
  8.       (setq plptlst (reverse plptlst))
  9.     )
  10.   )
  11.   (if plo
  12.     (progn
  13.       (mapcar '(lambda (x) (if (eq (car x) 10) (setq ploptlst (cons (cdr x) ploptlst)))) (entget plo))
  14.       (setq ploptlst (reverse ploptlst))
  15.       (entdel plo)
  16.     )
  17.   )
  18.   (setq k -1)
  19.   (if ploptlst
  20.     (progn
  21.       (repeat (length plptlst)
  22.         (setq k (1+ k))
  23.         (if (eq k (- (length plptlst) 1))
  24.           (setq ptt (inters (nth k plptlst) (nth k ploptlst) (nth 0 plptlst) (nth 0 ploptlst) nil))
  25.           (setq ptt (inters (nth k plptlst) (nth k ploptlst) (nth (+ k 1) plptlst) (nth (+ k 1) ploptlst) nil))
  26.         )
  27.         (if (and ptt (eq k (- (length plptlst) 1))) (setq pttlst (cons (distance (nth 0 plptlst) ptt) pttlst)))
  28.         (if (and ptt (not (eq k (- (length plptlst) 1)))) (setq pttlst (cons (distance (nth k plptlst) ptt) pttlst)))
  29.         (if (and ptt (< k (- (length plptlst) 1))) (setq pttlst (cons (distance (nth (+ k 1) plptlst) ptt) pttlst)))
  30.       )
  31.       (setq pttlstn (vl-sort pttlst '(lambda (a b) (< a b))))
  32.       (setq dst (car pttlstn))
  33.       (setq k -1)
  34.       (repeat (length plptlst)
  35.         (setq k (1+ k))
  36.         (if (eq k (- (length plptlst) 1))
  37.           (setq ptt (inters (nth k plptlst) (nth k ploptlst) (nth 0 plptlst) (nth 0 ploptlst) nil))
  38.           (setq ptt (inters (nth k plptlst) (nth k ploptlst) (nth (+ k 1) plptlst) (nth (+ k 1) ploptlst) nil))
  39.         )
  40.         (if (and ptt (not (eq k (- (length plptlst) 1))) (or (eq (distance (nth k plptlst) ptt) dst) (eq (distance (nth (+ k 1) plptlst) ptt) dst)))
  41.           (progn
  42.             (setq p ptt)
  43.             (vla-addline msp (vlax-3d-point (nth k plptlst)) (vlax-3d-point ptt))
  44.             (vla-addline msp (vlax-3d-point (nth (+ k 1) plptlst)) (vlax-3d-point ptt))
  45.           )
  46.           (if (and ptt (eq k (- (length plptlst) 1)) (or (eq (distance (nth k plptlst) ptt) dst) (eq (distance (nth 0 plptlst) ptt) dst)))
  47.             (progn        
  48.               (setq p ptt)
  49.               (vla-addline msp (vlax-3d-point (nth k plptlst)) (vlax-3d-point ptt))
  50.               (vla-addline msp (vlax-3d-point (nth 0 plptlst)) (vlax-3d-point ptt))
  51.             )
  52.           )
  53.         )
  54.       )
  55.     )
  56.   )
  57. )
  58.  
  59. (defun kr (plin / pold plinn pldxf plinptlst)
  60.   (setq pold p)
  61.   (vl-cmdf "_.offset" "t" plin p "")
  62.   (setq plin (entlast))
  63.   (setq pldxf (entget plin))
  64.   (mapcar '(lambda (x) (if (eq (car x) 10) (setq plinptlst (cons (cdr x) plinptlst)))) pldxf)
  65.   (setq plinptlst (reverse plinptlst))
  66.   (setq plinptlst (acet-list-remove-duplicates plinptlst 1e-6))
  67.   (entdel plin)
  68.   (setq plinn (entmakex (append (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline")
  69.                                       (cons 90 (length plinptlst))
  70.                                      '(70 . 1)
  71.                                 )
  72.                                 (mapcar '(lambda ( x ) (cons 10 x)) plinptlst)
  73.                                 (list '(210 0.0 0.0 1.0))
  74.                         )
  75.               )
  76.   )
  77.   (pll plinn)
  78.   (if (not (equal pold p 1e-8)) (kr plinn))
  79. )
  80.  
  81. (defun c:kr (/ pl)
  82.   (setq pl (car (entsel "\nPick 2d polyline")))
  83.   (pll pl)
  84.   (kr pl)
  85.   (princ)
  86. )
  87.  

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 02, 2012, 08:21:12 AM
As this is complicated to program, I watched this video (http://www.google.rs/url?sa=t&rct=j&q=ridges%20of%20sloped%20roof%20lisp&source=web&cd=11&ved=0CE0QtwIwADgK&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D2OHx4a-3VBQ&ei=awTKT-fTPO7O4QT9x7Ec&usg=AFQjCNGD597ZIfSugS7p57hZWbb0MfHPew&cad=rja) and suddenly everything revealed to be simply built-in AutoCAD...  :lmao:

This example will surely undercut Lee's business with the code selling... Maybe Lee would change his mind and post the code for learning purposes of us that don't know how to create *.lsp routine for this...

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: highflyingbird on June 02, 2012, 09:40:52 AM
This example will surely undercut Lee's business with the code selling... Maybe Lee would change his mind and post the code for learning purposes of us that don't know how to create *.lsp routine for this...

ribarm,excellent,I will take some time to study your code!
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: CAB on June 02, 2012, 11:31:44 AM
LeeMac has invested considerable time and effort in learning to code well. I think he has every right
to recoup something for his investment.  He has shared his talent freely in many threads so I feel
he has given plenty to our community.

My 2cents  8-)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 03, 2012, 06:34:02 AM
LeeMac has invested considerable time and effort in learning to code well. I think he has every right
to recoup something for his investment.  He has shared his talent freely in many threads so I feel
he has given plenty to our community.
Me too . :-)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: RGUS on June 03, 2012, 04:59:32 PM
I have a LISP solution which I believe yields the correct straight skeleton result for 90% of cases, but have not released it as freeware:

(http://www.theswamp.org/lilly_pond/leemac/Straight-Skeleton.gif)

Some more examples:

(http://www.theswamp.org/lilly_pond/leemac/Straight-Skeleton2.gif)

Awesome Lee... so where does one purchase such a cool bit of code?
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: CAB on June 03, 2012, 05:11:20 PM
BTW, This is the link to the old thread.
http://www.theswamp.org/index.php?topic=721.0
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: Lee Mac on June 04, 2012, 06:58:04 AM
Awesome Lee... so where does one purchase such a cool bit of code?

Thanks RGUS  :-)

The code isn't quite finished as yet, I've still to eradicate some minor bugs but I shall certainly inform you of its completion and availability when I've finished working on it.

Lee
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 04, 2012, 07:21:18 AM
But Lee, isn't this the same as yours (used cheat from video I posted)... Only thing is one more input of inside polyline point...

Quote
THE CODE REMOVED DUE IT'S UNNECESSARY - HAVEN'T GIVE CORRECT RESULTS UNDER A2008... CHECK MY POST BELOW WITH CODE CORRECT FOR A2008->A2012...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: CAB on June 04, 2012, 08:23:00 AM
Not working in ACAD2006 with simple rectangle.
Code: [Select]
Command: roof

Pick 2d polyline
Pick point inside 2d polyline
Enter choice (2D / 3D) : 2D
Unknown command "ROOF".  Press F1 for help.

Requires numeric distance, two points, or option keyword.


Unable to extrude the selected object.
Unknown command "ROOF".  Press F1 for help.
1 face found.


Modeling Operation Error:
     No solution for a vertex.
Unknown command "ROOF".  Press F1 for help.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 04, 2012, 08:51:20 AM
I am using A2008 and >... Can you upload your *.dwg with rectangle... Maybe cheat isn't functioning on A2006, but on my comps. it's working like charm...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: Faster on June 04, 2012, 09:12:59 AM
But Lee, isn't this the same as yours (used cheat from video I posted)... Only thing is one more input of inside polyline point...

...
M.R.
Nice code!Thanks!
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 04, 2012, 09:56:45 AM
Here is variant for A2008->A2012, and I suppose A2013...

Code - Auto/Visual Lisp: [Select]
  1. (defun ss=ss1-ss2 (ss1 ss2)
  2.   (acet-ss-remove ss2 ss1)
  3. )
  4.  
  5. (defun c:roof ( / ANG ANGG CH DELOB K LIN LINN OSM P P1 P2 PL PLL PP QAF S1 S2 SS SSS SSN VS)
  6.   (setq delob (getvar 'delobj))
  7.   (setq qaf (getvar 'qaflags))
  8.   (setq osm (getvar 'osmode))
  9.   (setvar 'osmode 0)
  10.   (setq pll (entsel "\nPick 2d polyline with straight segments"))
  11.   (setq pl (car pll))
  12.   (setq pp (cadr pll))
  13.   (setq p (getpoint "\nPick point inside 2d polyline"))
  14.   (setq p (list (car p) (cadr p) 1e-3))
  15.   (initget 7 "2D 3D")
  16.   (setq ch (getkword "\nEnter choice (2D / 3D) : "))
  17.   (if (eq ch "2D")
  18.     (progn
  19.       (setq s1 (ssget "_X" '((0 . "3DSOLID"))))
  20.       (vl-cmdf "_.regen")
  21.       (vl-cmdf "_.zoom" "v")
  22.       (setq vs (getvar 'viewsize))
  23.       (vl-cmdf "_.zoom" "p")
  24.       (setvar 'delobj 0)
  25.       (vl-cmdf "_.extrude" pl "" "t" 75.0 1e-3 "")
  26.       (vl-cmdf "_.solidedit" "f" "m" p "" (list 0.0 0.0 0.0) (list 0.0 0.0 vs) "" "")
  27.       (vl-cmdf "_.solidedit" "b" "p" (entlast) "" "")
  28.       (setq s2 (ssget "_X" '((0 . "3DSOLID"))))
  29.       (setq ss (ss=ss1-ss2 s2 s1))
  30.       (vl-cmdf "_.erase" ss "r" pp "")
  31.       (vl-cmdf "_.explode" (ssname (ssget pp '((0 . "3DSOLID"))) 0) "")
  32.       (setvar 'qaflags 1)
  33.       (vl-cmdf "_.explode" (ssget "_P") "")
  34.       (setq ss (ssget "_P"))
  35.       (setq sss (ssadd))
  36.       (repeat (setq ssn (sslength ss))
  37.         (setq lin (ssname ss (setq ssn (1- ssn))))
  38.         (if (and (eq (caddr (cdr (assoc 10 (entget lin)))) 0.0) (eq (caddr (cdr (assoc 11 (entget lin)))) 0.0))
  39.           (entdel lin)
  40.           (progn
  41.             (setq p1 (cdr (assoc 10 (entget lin))))
  42.             (setq p2 (cdr (assoc 11 (entget lin))))
  43.             (setq p1 (list (car p1) (cadr p1) 0.0))
  44.             (setq p2 (list (car p2) (cadr p2) 0.0))
  45.             (entmod (subst (cons 10 p1) (assoc 10 (entget lin)) (entget lin)))
  46.             (setq linn (entupd (cdr (assoc -1 (entmod (subst (cons 11 p2) (assoc 11 (entget lin)) (entget lin)))))))
  47.             (ssadd linn sss)
  48.           )
  49.         )
  50.       )
  51.       (repeat (setq ssn (sslength sss))
  52.         (setq lin (ssname sss (setq ssn (1- ssn))))
  53.         (repeat (setq k ssn)
  54.           (setq linn (ssname sss (setq k (1- k))))
  55.           (if (or (and (equal (assoc 10 (entget lin)) (assoc 10 (entget linn)) 1e-6) (equal (assoc 11 (entget lin)) (assoc 11 (entget linn)) 1e-6)) (and (equal (assoc 10 (entget lin)) (assoc 11 (entget linn)) 1e-6) (equal (assoc 11 (entget lin)) (assoc 10 (entget linn)) 1e-6)))
  56.             (entdel lin)
  57.           )
  58.         )
  59.       )
  60.     )
  61.     (progn
  62.       (setq s1 (ssget "_X" '((0 . "3DSOLID"))))
  63.       (setvar 'delobj 0)
  64.       (setq ang (getreal "\nEnter angle of slope of roof (0 < ang < 90) : "))
  65.       (setq angg (- 90.0 ang))
  66.       (vl-cmdf "_.regen")
  67.       (vl-cmdf "_.zoom" "v")
  68.       (setq vs (getvar 'viewsize))
  69.       (vl-cmdf "_.zoom" "p")
  70.       (vl-cmdf "_.extrude" pl "" "t" angg 1e-3 "")
  71.       (vl-cmdf "_.solidedit" "f" "m" p "" (list 0.0 0.0 0.0) (list 0.0 0.0 vs) "" "")
  72.       (vl-cmdf "_.solidedit" "b" "p" (entlast) "" "")
  73.       (setq s2 (ssget "_X" '((0 . "3DSOLID"))))
  74.       (setq ss (ss=ss1-ss2 s2 s1))
  75.       (vl-cmdf "_.erase" ss "r" pp "")
  76.     )
  77.   )
  78.   (setvar 'delobj delob)
  79.   (setvar 'qaflags qaf)
  80.   (setvar 'osmode osm)
  81.   (princ)
  82. )
  83.  

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: LE3 on June 04, 2012, 10:10:52 AM
Nothing new or deja voo? - dang! have been a long time - time pass fast indeed - and old thinkings become new ones.... nice to go back on time with this..... keep the good work swamp guys!!!!
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: CAB on June 04, 2012, 12:15:50 PM
Nice to hear from you Luis.  8-)

MR I think the problem is with the extrude command in 2006.
Code: [Select]
Command: extrude

Current wire frame density:  ISOLINES=4
Select objects: 1 found

Select objects:

Specify height of extrusion or [Path]:  Specify second point:
Specify angle of taper for extrusion <0>: 45
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 04, 2012, 01:17:21 PM
Which One is right ?
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: TimSpangler on June 04, 2012, 01:47:36 PM
Which One is right ?

The one on the left
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: CAB on June 04, 2012, 01:58:32 PM
The one on the right has a "Dead Vally" (a flat valley). This is to be avoided but sometime can not.
When you can't get away from the situation you add a "Cricket"  8-)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 05, 2012, 06:23:21 AM
The code for roof finally revised... If you plan to use pline with arcs, I suggest segmentation... This one was by Lee Mac, and I just modified it to suite my needs... Thanks, Lee...

Code - Auto/Visual Lisp: [Select]
  1. ;; LWPolyline to Point List  -  Lee Mac
  2. ;; Returns a list of points describing the supplied LWPolyline
  3.  
  4. (defun LM:LWPoly->List ( ent n / der di1 di2 inc lst par rad )
  5.     (setq par 0)
  6.     (repeat (cdr (assoc 90 (entget ent)))
  7.         (if (setq der (vlax-curve-getsecondderiv ent par))
  8.             (if (equal der '(0.0 0.0 0.0) 1e-8)
  9.                 (setq lst (cons (vlax-curve-getpointatparam ent par) lst))
  10.                 (if
  11.                     (setq rad (distance '(0.0 0.0) (vlax-curve-getfirstderiv ent par))
  12.                           di1 (vlax-curve-getdistatparam ent par)
  13.                           di2 (vlax-curve-getdistatparam ent (1+ par))
  14.                     )
  15.                     (progn
  16.                         (setq inc (/ (- di2 di1) n))
  17.                         (while (< di1 di2)
  18.                             (setq lst (cons (vlax-curve-getpointatdist ent di1) lst)
  19.                                   di1 (+ di1 inc)
  20.                             )
  21.                         )
  22.                     )
  23.                 )
  24.             )
  25.         )
  26.         (setq par (1+ par))
  27.     )
  28.     lst
  29. )
  30.  
  31. ;; Test Function
  32.  
  33. (defun c:test ( / en lst n )
  34.     (while
  35.         (progn (setvar 'ERRNO 0) (setq en (car (entsel "\nSelect LWPolyline: ")))
  36.             (cond
  37.                 (   (= 7 (getvar 'ERRNO))
  38.                     (princ "\nMissed, try again.")
  39.                 )
  40.                 (   (eq 'ENAME (type en))
  41.                     (if (not (eq "LWPOLYLINE" (cdr (assoc 0 (entget en)))))
  42.                         (princ "\nInvalid Object Selected.")
  43.                     )
  44.                 )
  45.             )
  46.         )
  47.     )
  48.     (initget 6)
  49.     (setq n (getint "\nInput number of curved segments per arc <25> : "))
  50.     (if (null n) (setq n 25))
  51.     (if
  52.         (and en
  53.             (setq lst
  54.                 (mapcar
  55.                     (function (lambda ( x ) (trans x 0 en)))
  56.                     (LM:LWPoly->List en n)
  57.                 )
  58.             )
  59.         )
  60.         (entmakex
  61.             (append
  62.                 (list
  63.                     (cons 0 "LWPOLYLINE")
  64.                     (cons 100 "AcDbEntity")
  65.                     (cons 100 "AcDbPolyline")
  66.                     (cons 62 3)
  67.                     (cons 90 (length lst))
  68.                     (assoc 70 (entget en))
  69.                     (assoc 210 (entget en))
  70.                 )
  71.                 (mapcar '(lambda ( x ) (cons 10 x)) lst)
  72.             )
  73.         )
  74.     )
  75.     (princ)
  76. )
  77.  
  78.  

P.S. It can happen that CAD can't do extrude with taper angle, and then this roof.lsp fails, but in most cases with not to complex plines it works fine... Hope that CAB solved the issue with extrude in A2006 (don't have it installed, so I can't check my code)

Sincerely, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: CAB on June 05, 2012, 08:43:15 AM
Changing to this extrude (vl-cmdf "_.extrude" pl "" 1000 45.0)
produces this:
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 05, 2012, 10:48:02 AM
CAB, have you tried what's shown on this video (http://www.google.rs/url?sa=t&rct=j&q=ridges%20of%20sloped%20roof%20lisp&source=web&cd=11&ved=0CE0QtwIwADgK&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D2OHx4a-3VBQ&ei=awTKT-fTPO7O4QT9x7Ec&usg=AFQjCNGD597ZIfSugS7p57hZWbb0MfHPew&cad=rja)... How is CAD behaving with SOLIDEDIT -> face -> move (vertex1 -> .xy vertex1 (z=1000)) in view ISOmetric (vpoint 1,1,1) ? Can it compute roof solution from your second example (picture) ? If not than routine will fail (contact Lee Mac) or witch I strongly suggest (A2006)=>A2008 or more up to date...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 07, 2012, 11:14:20 AM
One last revision on code :
http://www.theswamp.org/index.php?topic=41837.msg470185#msg470185

(no need for calculation of area of polyline), just use variable VS (getvar 'vievsize) of (vl-cmdf "_.zoom" "v") view for determining Z most limit of moving face with solidedit along z axis...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: GDF on June 08, 2012, 10:12:43 AM
Very nice. Works perfectly.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 03, 2013, 09:30:47 AM
I've just finished my newest version of 2droof... In some cases with unorthogonal edges it is fine, but in some cases it can't construct correct finish apex ridge... Nevertheless as this version is freeware - my code, maybe it can be used to compare results with my version witch uses extrude command...

So here is it... I am waiting to see reactions, maybe someone solve this for orthogonal edges and correct finish and for concave edges...

[EDIT]: Code changed to be working correctly, but only with polylines that have straight unorthogonal convex segments-edges

Code - Auto/Visual Lisp: [Select]
  1. (defun c:2droof-MR (/      unique _vl-remove    ridge  onlin-p
  2.                     assocon       prelst suflst unit   kr     ll
  3.                     pl     pln    poly   rl     tl     v      v1
  4.                     v2     vl     vp     vp1    vp2    vpp1   vpp2
  5.                     vpl    vrl    vtl    vx
  6.                    )
  7.  
  8.   (defun unique (lst)
  9.     (if lst
  10.       (cons (car lst)
  11.             (unique (_vl-remove (car lst) (cdr lst) 1e-6))
  12.       )
  13.     )
  14.   )
  15.  
  16.   (defun _vl-remove (el lst fuzz)
  17.     (vl-remove-if
  18.       '(lambda (x)
  19.          (and (equal (car x) (car el) fuzz)
  20.               (equal (cadr x) (cadr el) fuzz)
  21.          )
  22.        )
  23.       lst
  24.     )
  25.   )
  26.  
  27.   (defun ridge (v1 v2)
  28.     (if (not
  29.           (minusp (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1))))
  30.         )
  31.       (mapcar '*
  32.               (list -1.0 -1.0 -1.0)
  33.               (mapcar '- v1 v2)
  34.       )
  35.       (if (or (equal (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1)))
  36.                      0.0
  37.                      1e-8
  38.               )
  39.               (equal (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1)))
  40.                      -0.0
  41.                      -1e-8
  42.               )
  43.           )
  44.           (if (equal v1 v2 1e-8)
  45.             (polar '(0.0 0.0 0.0)
  46.                    (+ (angle '(0.0 0.0 0.0) v1) (/ pi 2.0))
  47.                    1.0
  48.             )
  49.             v2
  50.           )
  51.         (mapcar '- v1 v2)
  52.       )
  53.     )
  54.   )
  55.  
  56.   (defun onlin-p (p1 p2 p)
  57.     (and
  58.       (equal (distance p1 p2)
  59.              (+ (distance p1 p) (distance p2 p))
  60.              1e-6
  61.       )
  62.       (not (equal (distance p1 p) 0.0 1e-6))
  63.       (not (equal (distance p2 p) 0.0 1e-6))
  64.     )
  65.   )
  66.  
  67.   (defun assocon (SearchTerm Lst func fuzz)
  68.     (car
  69.       (vl-member-if
  70.         (function
  71.           (lambda (pair)
  72.             (equal SearchTerm (apply func (list pair)) fuzz)
  73.           )
  74.         )
  75.         lst
  76.       )
  77.     )
  78.   )
  79.  
  80.   (defun prelst (lst el / f)
  81.     (vl-remove-if
  82.       '(lambda (a) (or f (setq f (equal a el 1e-8))))
  83.       lst
  84.     )
  85.   )
  86.  
  87.   (defun suflst (lst el)
  88.     (cdr (vl-member-if '(lambda (a) (equal a el 1e-8)) lst))
  89.   )
  90.  
  91.   (defun unit (v)
  92.     (mapcar '(lambda (x) (/ x (distance '(0.0 0.0 0.0) v))) v)
  93.   )
  94.  
  95.  
  96.   )
  97.  
  98.   (setq poly
  99.          (car
  100.            (entsel
  101.              "\nPick LWPOLYLINE in WCS oriented CCW and with only straight convex segments-edges"
  102.            )
  103.          )
  104.   )
  105.   (setq
  106.     vl (mapcar
  107.          'cdr
  108.          (vl-remove-if-not
  109.            '(lambda (x) (= (car x) 10))
  110.            (entget poly)
  111.          )
  112.        )
  113.   )
  114.   (setq vl (cons (last vl) vl))
  115.   (setq tl (mapcar '(lambda (a b) (mapcar '- b a)) vl (cdr vl)))
  116.   (setq tl (mapcar '(lambda (x) (unit x)) tl))
  117.   (setq vtl (mapcar '(lambda (a b) (list a b)) vl tl))
  118.   (setq rl (mapcar '(lambda (a b) (ridge a b))
  119.                    tl
  120.                    (cdr (reverse (cons (car tl) (reverse tl))))
  121.            )
  122.   )
  123.   (setq rl (mapcar '(lambda (x) (unit x)) rl))
  124.   (setq vrl (mapcar '(lambda (a b) (list a b))
  125.                     (cdr (reverse (cons (car vl) (reverse vl))))
  126.                     rl
  127.             )
  128.   )
  129.   (setq vrl (cons (last vrl) (reverse (cdr (reverse vrl)))))
  130.   (setq pln T)
  131.  
  132.   (defun kr (lst / pl pp)
  133.     (setq pl (mapcar '(lambda (a b)
  134.                         (inters (car a)
  135.                                 (mapcar '+ (car a) (cadr a))
  136.                                 (car b)
  137.                                 (mapcar '+ (car b) (cadr b))
  138.                                 nil
  139.                         )
  140.                       )
  141.                      (reverse (cons (car lst) (reverse lst)))
  142.                      (cdr (reverse (cons (car lst) (reverse lst))))
  143.              )
  144.     )
  145.     (setq vpl (mapcar '(lambda (a b c) (list (list a b) (list c b)))
  146.                       vl
  147.                       pl
  148.                       (cdr vl)
  149.               )
  150.     )
  151.     (setq vpl (apply 'append vpl))
  152.     (while (assocon nil vpl 'cadr 1e-6)
  153.       (setq vpl (vl-remove (assocon nil vpl 'cadr 1e-6) vpl))
  154.     )
  155.     (setq pln nil)
  156.     (foreach p pl
  157.       (if (vl-some
  158.             '(lambda (x)
  159.                (vl-catch-all-apply 'onlin-p (list (car x) (cadr x) p))
  160.              )
  161.             vpl
  162.           )
  163.         (setq pln (cons p pln))
  164.       )
  165.     )
  166.     (foreach p (reverse pln)
  167.       (if (vl-some
  168.             '(lambda (x)
  169.                (vl-catch-all-apply 'onlin-p (list (car x) p (cadr x)))
  170.              )
  171.             vpl
  172.           )
  173.         (setq pln (vl-remove-if '(lambda (x) (equal x p 1e-6)) pln))
  174.       )
  175.     )
  176.     (setq vx nil)
  177.     (foreach p pln
  178.       (mapcar '(lambda (x)
  179.                  (if (equal (cadr x) p 1e-6)
  180.                    (setq vx (cons x vx))
  181.                  )
  182.                )
  183.               vpl
  184.       )
  185.     )
  186.     (setq pln (list (cadar (vl-sort vx
  187.                                     '(lambda (a b)
  188.                                        (< (distance (car a) (cadr a))
  189.                                           (distance (car b) (cadr b))
  190.                                        )
  191.                                      )
  192.                            )
  193.                     )
  194.               )
  195.     )
  196.     (if (vl-every '(lambda (x) (equal (cadar vpl) (cadr x) 1e-6))
  197.                   vpl
  198.         )
  199.       (progn
  200.         (foreach l (unique vpl)
  201.           (entmake (list '(0 . "LINE")
  202.                          (cons 10 (car l))
  203.                          (cons 11 (cadr l))
  204.                    )
  205.           )
  206.         )
  207.         (setq pln nil)
  208.       )
  209.     )
  210.     (foreach p pln
  211.       (setq vp1 (cons (assocon p vpl 'cadr 1e-6) vp1))
  212.       (setq vp2
  213.              (cons (assocon p (cdr (member (car vp1) vpl)) 'cadr 1e-6)
  214.                    vp2
  215.              )
  216.       )
  217.       (if (car vp1)
  218.         (entmake (list '(0 . "LINE")
  219.                        (cons 10 (caar vp1))
  220.                        (cons 11 (cadar vp1))
  221.                  )
  222.         )
  223.       )
  224.       (if (car vp2)
  225.         (entmake (list '(0 . "LINE")
  226.                        (cons 10 (caar vp2))
  227.                        (cons 11 (cadar vp2))
  228.                  )
  229.         )
  230.       )
  231.       (setq vpp2 (caar vp2))
  232.       (setq v2 nil)
  233.       (while (setq vpp2 (car (assocon vpp2 vp2 'cadr 1e-6)))
  234.         (if (not (null vpp2))
  235.           (setq v2 vpp2)
  236.         )
  237.       )
  238.       (if (null v2)
  239.         (setq v2 (caar vp2))
  240.       )
  241.       (setq vpp1 (caar vp1))
  242.       (setq v1 nil)
  243.       (while (setq vpp1 (car (assocon vpp1 vp1 'cadr 1e-6)))
  244.         (if (not (null vpp1))
  245.           (setq v1 vpp1)
  246.         )
  247.       )
  248.       (if (null v1)
  249.         (setq v1 (caar vp1))
  250.       )
  251.       (setq pp
  252.              (list
  253.                p
  254.                (unit
  255.                  (ridge
  256.                    (if
  257.                      (cadr
  258.                        (last (prelst vtl (assocon v1 vtl 'car 1e-6)))
  259.                      )
  260.                       (cadr
  261.                         (last (prelst vtl (assocon v1 vtl 'car 1e-6)))
  262.                       )
  263.                       (cadr (last vtl))
  264.                    )
  265.                    (cadr (assocon v2 vtl 'car 1e-6))
  266.                  )
  267.                )
  268.              )
  269.       )
  270.       (setq vrl (if vrl
  271.                   (subst pp (assocon (caar vp1) vrl 'car 1e-6) vrl)
  272.                   (subst pp (assocon (caar vp1) lst 'car 1e-6) lst)
  273.                 )
  274.             vrl (if (assocon (caar vp2) vrl 'car 1e-6)
  275.                   (subst pp (assocon (caar vp2) vrl 'car 1e-6) vrl)
  276.                 )
  277.       )
  278.       (setq vl (subst p (caar vp1) vl)
  279.             vl (subst p (caar vp2) vl)
  280.       )
  281.     )
  282.   )
  283.  
  284.   (while pln (kr vrl))
  285.  
  286.   )
  287.  
  288.   (princ)
  289. )
  290.  

Regards, M.R.
 8-) 8-) 8-)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 03, 2013, 02:10:35 PM
Code changed little...

Attached is dwg showing where isn't working well...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 04, 2013, 03:05:37 AM
Now it's working fine in all cases in witch polyline is drawn CCW and that have straight unorthogonal convex edges-segements...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 05, 2013, 03:01:26 AM
Now it's working fine in all cases in witch polyline is drawn CCW and that have straight unorthogonal convex edges-segements...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 06, 2013, 02:59:33 AM
Here is my newest version - food for thoughts...

I must warn you, it may produce incorrect results when reaching connections of peripheral tree branches of more complex plines... Also above attached example won't even do anything - it's too complex...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:2droof-MR (/      unique _vl-remove    ridge  onlin-p
  2.                     assocon       prelst suflst unit   kr     ll
  3.                     pl     pln    poly   rl     tl     v      v1
  4.                     v2     vl     vp     vp1    vp2    vpp1   vpp2
  5.                     vpl    vrl    vtl    vx
  6.                    )
  7.  
  8.   (defun unique (lst)
  9.     (if lst
  10.       (cons (car lst)
  11.             (unique (_vl-remove (car lst) (cdr lst) 1e-6))
  12.       )
  13.     )
  14.   )
  15.  
  16.   (defun _vl-remove (el lst fuzz)
  17.     (vl-remove-if
  18.       '(lambda (x)
  19.          (and (equal (car x) (car el) fuzz)
  20.               (equal (cadr x) (cadr el) fuzz)
  21.          )
  22.        )
  23.       lst
  24.     )
  25.   )
  26.  
  27.   (defun ridge (v1 v2)
  28.     (if (not
  29.           (minusp (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1))))
  30.         )
  31.       (mapcar '*
  32.               (list -1.0 -1.0 -1.0)
  33.               (mapcar '- v1 v2)
  34.       )
  35.       (if (or (equal (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1)))
  36.                      0.0
  37.                      1e-8
  38.               )
  39.               (equal (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1)))
  40.                      -0.0
  41.                      -1e-8
  42.               )
  43.           )
  44.         (if (equal v1 v2 1e-8)
  45.           (polar '(0.0 0.0 0.0)
  46.                  (+ (angle '(0.0 0.0 0.0) v1) (/ pi 2.0))
  47.                  1.0
  48.           )
  49.           v2
  50.         )
  51.         (mapcar '- v1 v2)
  52.       )
  53.     )
  54.   )
  55.  
  56.   (defun onlin-p (p1 p2 p)
  57.     (and
  58.       (equal (distance p1 p2)
  59.              (+ (distance p1 p) (distance p2 p))
  60.              1e-6
  61.       )
  62.       (not (equal (distance p1 p) 0.0 1e-6))
  63.       (not (equal (distance p2 p) 0.0 1e-6))
  64.     )
  65.   )
  66.  
  67.   (defun assocon (SearchTerm Lst func fuzz)
  68.     (car
  69.       (vl-member-if
  70.         (function
  71.           (lambda (pair)
  72.             (equal SearchTerm (apply func (list pair)) fuzz)
  73.           )
  74.         )
  75.         lst
  76.       )
  77.     )
  78.   )
  79.  
  80.   (defun prelst (lst el / f)
  81.     (vl-remove-if
  82.       '(lambda (a) (or f (setq f (equal a el 1e-8))))
  83.       lst
  84.     )
  85.   )
  86.  
  87.   (defun suflst (lst el)
  88.     (cdr (vl-member-if '(lambda (a) (equal a el 1e-8)) lst))
  89.   )
  90.  
  91.   (defun unit (v)
  92.     (mapcar '(lambda (x) (/ x (distance '(0.0 0.0 0.0) v))) v)
  93.   )
  94.  
  95.  
  96.   )
  97.  
  98.   (setq poly
  99.          (car
  100.            (entsel
  101.              "\nPick LWPOLYLINE in WCS oriented CCW and with only straight convex segments-edges"
  102.            )
  103.          )
  104.   )
  105.   (setq
  106.     vl (mapcar
  107.          'cdr
  108.          (vl-remove-if-not
  109.            '(lambda (x) (= (car x) 10))
  110.            (entget poly)
  111.          )
  112.        )
  113.   )
  114.   (setq vl (cons (last vl) vl))
  115.   (setq tl (mapcar '(lambda (a b) (mapcar '- b a)) vl (cdr vl)))
  116.   (setq tl (mapcar '(lambda (x) (unit x)) tl))
  117.   (setq vtl (mapcar '(lambda (a b) (list a b)) vl tl))
  118.   (setq rl (mapcar '(lambda (a b) (ridge a b))
  119.                    tl
  120.                    (cdr (reverse (cons (car tl) (reverse tl))))
  121.            )
  122.   )
  123.   (setq rl (mapcar '(lambda (x) (unit x)) rl))
  124.   (setq vrl (mapcar '(lambda (a b) (list a b))
  125.                     (cdr (reverse (cons (car vl) (reverse vl))))
  126.                     rl
  127.             )
  128.   )
  129.   (setq vrl (cons (last vrl) (reverse (cdr (reverse vrl)))))
  130.   (setq pln T)
  131.  
  132.   (defun kr (lst / pl plnn plnnn pll plll vpx1 vpxp1 vpx2 vpxp2 pp)
  133.     (mapcar
  134.       '(lambda (a b)
  135.          (setq p
  136.                 (inters (car a)
  137.                         (mapcar '+ (car a) (cadr a))
  138.                         (car b)
  139.                         (mapcar '+ (car b) (cadr b))
  140.                         nil
  141.                 )
  142.          )
  143.          (setq pl (cons p pl))
  144.          (if
  145.            (and
  146.              (vl-catch-all-apply
  147.                'onlin-p
  148.                (list (car a) p (mapcar '+ (car a) (cadr a)))
  149.              )
  150.  
  151.              (vl-catch-all-apply
  152.                'onlin-p
  153.                (list (car b) p (mapcar '+ (car b) (cadr b)))
  154.              )
  155.            )
  156.             (setq pll (cons p pll))
  157.          )
  158.          (if
  159.            (and
  160.              (vl-catch-all-apply
  161.                'onlin-p
  162.                (list (mapcar '+ (car a) (cadr a)) p (car a))
  163.              )
  164.  
  165.              (vl-catch-all-apply
  166.                'onlin-p
  167.                (list (mapcar '+ (car b) (cadr b)) p (car b))
  168.              )
  169.            )
  170.             (setq plll (cons p plll))
  171.          )
  172.        )
  173.       (reverse (cons (car lst) (reverse lst)))
  174.       (cdr (reverse (cons (car lst) (reverse lst))))
  175.     )
  176.     (setq vpl (mapcar '(lambda (a b c) (list (list a b) (list c b)))
  177.                       vl
  178.                       (reverse pl)
  179.                       (cdr vl)
  180.               )
  181.     )
  182.     (setq vpl (apply 'append vpl))
  183.     (while (assocon nil vpl 'cadr 1e-6)
  184.       (setq vpl (vl-remove (assocon nil vpl 'cadr 1e-6) vpl))
  185.     )
  186.     (setq pln nil)
  187.     (foreach p pl
  188.       (if (vl-some
  189.             '(lambda (x)
  190.                (vl-catch-all-apply 'onlin-p (list (car x) (cadr x) p))
  191.              )
  192.             vpl
  193.           )
  194.         (setq pln (cons p pln))
  195.       )
  196.     )
  197.     (foreach p (reverse pln)
  198.       (if (vl-some
  199.             '(lambda (x)
  200.                (vl-catch-all-apply 'onlin-p (list (car x) p (cadr x)))
  201.              )
  202.             vpl
  203.           )
  204.         (setq pln (vl-remove-if '(lambda (x) (equal x p 1e-6)) pln))
  205.       )
  206.     )
  207.     (foreach p pl
  208.       (setq vpx1 (assocon p vpl 'cadr 1e-6))
  209.       (setq vpxp1 (if (last (prelst vpl vpx1))
  210.                     (last (prelst vpl vpx1))
  211.                     (last vpl)
  212.                   )
  213.       )
  214.       (if (equal (car vpx1) (car vpxp1) 1e-6)
  215.         (setq vpxp1 (if (last (prelst vpl vpxp1))
  216.                       (last (prelst vpl vpxp1))
  217.                       (last vpl)
  218.                     )
  219.         )
  220.       )
  221.       (setq vpx2 (assocon p (vl-remove vpx1 vpl) 'cadr 1e-6))
  222.       (setq vpxp2 (if (car (suflst vpl vpx2))
  223.                     (car (suflst vpl vpx2))
  224.                     (car vpl)
  225.                   )
  226.       )
  227.       (if (equal (car vpx2) (car vpxp2) 1e-6)
  228.         (setq vpxp2 (if (car (suflst vpl vpxp2))
  229.                       (car (suflst vpl vpxp2))
  230.                       (car vpl)
  231.                     )
  232.         )
  233.       )
  234.       (if (or (and (equal (unit (mapcar '- (cadr vpx1) (car vpx1)))
  235.                           (unit (mapcar '- (car vpxp1) (cadr vpxp1)))
  236.                           1e-6
  237.                    )
  238.                    (equal (unit (mapcar '- (cadr vpx2) (car vpx2)))
  239.                           (unit (mapcar '- (car vpxp2) (cadr vpxp2)))
  240.                           1e-6
  241.                    )
  242.               )
  243.               (and (equal (unit (mapcar '- (cadr vpx1) (car vpx1)))
  244.                           (unit (mapcar '- (cadr vpxp1) (car vpxp1)))
  245.                           1e-6
  246.                    )
  247.                    (equal (unit (mapcar '- (cadr vpx2) (car vpx2)))
  248.                           (unit (mapcar '- (cadr vpxp2) (car vpxp2)))
  249.                           1e-6
  250.                    )
  251.               )
  252.               (and (equal (unit (mapcar '- (cadr vpx1) (car vpx1)))
  253.                           (unit (mapcar '- (car vpxp1) (cadr vpxp1)))
  254.                           1e-6
  255.                    )
  256.                    (equal (unit (mapcar '- (cadr vpx2) (car vpx2)))
  257.                           (unit (mapcar '- (cadr vpxp2) (car vpxp2)))
  258.                           1e-6
  259.                    )
  260.               )
  261.               (and (equal (unit (mapcar '- (cadr vpx1) (car vpx1)))
  262.                           (unit (mapcar '- (cadr vpxp1) (car vpxp1)))
  263.                           1e-6
  264.                    )
  265.                    (equal (unit (mapcar '- (cadr vpx2) (car vpx2)))
  266.                           (unit (mapcar '- (car vpxp2) (cadr vpxp2)))
  267.                           1e-6
  268.                    )
  269.               )
  270.           )
  271.         (setq plnn (cons p plnn))
  272.       )
  273.     )
  274.     (foreach p plnn
  275.       (if (vl-member-if '(lambda (x) (equal x p 1e-6)) pll)
  276.         (setq plnnn (cons p plnnn))
  277.       )
  278.     )
  279.     (setq pln (append pln plnnn))
  280.     (setq vx nil)
  281.     (foreach p pln
  282.       (mapcar '(lambda (x)
  283.                  (if (equal (cadr x) p 1e-6)
  284.                    (setq vx (cons x vx))
  285.                  )
  286.                )
  287.               vpl
  288.       )
  289.     )
  290.     (foreach p plll
  291.       (mapcar '(lambda (x)
  292.                  (if (equal (cadr x) p 1e-6)
  293.                    (setq vx (vl-remove x vx))
  294.                  )
  295.                )
  296.               vx
  297.       )
  298.     )      
  299.     (setq pln (list (cadar
  300.                       (vl-sort vx
  301.                                '(lambda (a b)
  302.                                   (< (distance (car a) (cadr a))
  303.                                      (distance (car b) (cadr b))
  304.                                   )
  305.                                 )
  306.                       )
  307.                     )
  308.               )
  309.     )
  310.     (if (eq pln nil)
  311.       (setq pln (acet-list-remove-duplicates (mapcar 'cadr vpl) 1e-6))
  312.     )
  313.     (if (vl-every '(lambda (x) (equal (cadar vpl) (cadr x) 1e-6))
  314.                   vpl
  315.         )
  316.       (progn
  317.         (foreach l (unique vpl)
  318.           (entmake (list '(0 . "LINE")
  319.                          (cons 10 (car l))
  320.                          (cons 11 (cadr l))
  321.                    )
  322.           )
  323.         )
  324.         (setq pln nil)
  325.       )
  326.     )
  327.     (if (and (eq vpl nil) (= (length (unique vrl)) 2))
  328.       (entmake (list '(0 . "LINE")
  329.                      (cons 10 (caar (unique vrl)))
  330.                      (cons 11 (caadr (unique vrl)))
  331.                )
  332.       )
  333.     )
  334.     (foreach p pln
  335.       (setq vp1 (cons (assocon p vpl 'cadr 1e-6) vp1))
  336.       (setq vp2
  337.              (cons (assocon p (cdr (member (car vp1) vpl)) 'cadr 1e-6)
  338.                    vp2
  339.              )
  340.       )
  341.       (if (car vp1)
  342.         (entmake (list '(0 . "LINE")
  343.                        (cons 10 (caar vp1))
  344.                        (cons 11 (cadar vp1))
  345.                  )
  346.         )
  347.       )
  348.       (if (car vp2)
  349.         (entmake (list '(0 . "LINE")
  350.                        (cons 10 (caar vp2))
  351.                        (cons 11 (cadar vp2))
  352.                  )
  353.         )
  354.       )
  355.       (setq vpp2 (caar vp2))
  356.       (setq v2 nil)
  357.       (while (setq vpp2 (car (assocon vpp2 vp2 'cadr 1e-6)))
  358.         (if (not (null vpp2))
  359.           (setq v2 vpp2)
  360.         )
  361.       )
  362.       (if (null v2)
  363.         (setq v2 (caar vp2))
  364.       )
  365.       (setq vpp1 (caar vp1))
  366.       (setq v1 nil)
  367.       (while (setq vpp1 (car (assocon vpp1 vp1 'cadr 1e-6)))
  368.         (if (not (null vpp1))
  369.           (setq v1 vpp1)
  370.         )
  371.       )
  372.       (if (null v1)
  373.         (setq v1 (caar vp1))
  374.       )
  375.       (setq pp
  376.              (list
  377.                p
  378.                (unit
  379.                  (ridge
  380.                    (if
  381.                      (cadr
  382.                        (last (prelst vtl (assocon v1 vtl 'car 1e-6)))
  383.                      )
  384.                       (cadr
  385.                         (last (prelst vtl (assocon v1 vtl 'car 1e-6)))
  386.                       )
  387.                       (cadr (last vtl))
  388.                    )
  389.                    (cadr (assocon v2 vtl 'car 1e-6))
  390.                  )
  391.                )
  392.              )
  393.       )
  394.       (setq vrl (if vrl
  395.                   (subst pp (assocon (caar vp1) vrl 'car 1e-6) vrl)
  396.                   (subst pp (assocon (caar vp1) lst 'car 1e-6) lst)
  397.                 )
  398.             vrl (if (assocon (caar vp2) vrl 'car 1e-6)
  399.                   (subst pp (assocon (caar vp2) vrl 'car 1e-6) vrl)
  400.                 )
  401.       )
  402.       (setq vl (subst p (caar vp1) vl)
  403.             vl (subst p (caar vp2) vl)
  404.       )
  405.     )
  406.   )
  407.  
  408.   (while pln (kr vrl))
  409.  
  410.   )
  411.  
  412.   (princ)
  413. )
  414.  

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 06, 2013, 10:34:56 PM
Here is my final code... If it breaks on some point, then you must reconstruct roof solution manually based on received result... This is rare case and it happens with more complex roofs... Note that roof must have one single solution of connected ridges; it will fail if you have small roof dormers as additions to main polyline... Now it should work and with concave plines... Any shape is possible... It solved my earlier situation posted above... Now there is another one - see attachment, but as I said, you'll have to reconstruct solution manually...

[EDIT]: Code finally updated - it should solve in any situation...

Code - Auto/Visual Lisp: [Select]
  1. (defun 2droof (pol    /      _reml  unique _vl-remove    ridge  onlin-p
  2.                assocon       prelst suflst ll     pl     pln    i
  3.                rl     tl     v      v1     v2     vl     vp     vp1
  4.                vp2    vpp1   vpp2   vpl    vrl    vtl    vx
  5.               )
  6.  
  7.   (defun _reml (l1 l2 / a n ls)
  8.     (while
  9.       (setq n nil
  10.             a (car l2)
  11.       )
  12.        (while (and l1 (null n))
  13.          (if (equal a (car l1) 1e-8)
  14.            (setq l1 (cdr l1)
  15.                  n  t
  16.            )
  17.            (setq ls (append ls (list (car l1)))
  18.                  l1 (cdr l1)
  19.            )
  20.          )
  21.        )
  22.        (setq l2 (cdr l2))
  23.     )
  24.     (append ls l1)
  25.   )
  26.  
  27.   (defun unique (lst)
  28.     (if lst
  29.       (cons (car lst)
  30.             (unique (_vl-remove (car lst) (cdr lst) 1e-6))
  31.       )
  32.     )
  33.   )
  34.  
  35.   (defun _vl-remove (el lst fuzz)
  36.     (vl-remove-if
  37.       '(lambda (x)
  38.          (and (equal (car x) (car el) fuzz)
  39.               (equal (cadr x) (cadr el) fuzz)
  40.          )
  41.        )
  42.       lst
  43.     )
  44.   )
  45.  
  46.   (defun ridge (v1 v2)
  47.     (if (not
  48.           (minusp (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1))))
  49.         )
  50.       (mapcar '*
  51.               (list -1.0 -1.0 -1.0)
  52.               (mapcar '- v1 v2)
  53.       )
  54.       (if (or (equal (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1)))
  55.                      0.0
  56.                      1e-8
  57.               )
  58.               (equal (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1)))
  59.                      -0.0
  60.                      -1e-8
  61.               )
  62.           )
  63.         (if (equal v1 v2 1e-8)
  64.           (polar '(0.0 0.0 0.0)
  65.                  (+ (angle '(0.0 0.0 0.0) v1) (/ pi 2.0))
  66.                  1.0
  67.           )
  68.           v2
  69.         )
  70.         (mapcar '- v1 v2)
  71.       )
  72.     )
  73.   )
  74.  
  75.   (defun onlin-p (p1 p2 p)
  76.     (and
  77.       (equal (distance p1 p2)
  78.              (+ (distance p1 p) (distance p2 p))
  79.              1e-6
  80.       )
  81.       (not (equal (distance p1 p) 0.0 1e-6))
  82.       (not (equal (distance p2 p) 0.0 1e-6))
  83.     )
  84.   )
  85.  
  86.   (defun assocon (SearchTerm Lst func fuzz)
  87.     (car
  88.       (vl-member-if
  89.         (function
  90.           (lambda (pair)
  91.             (equal SearchTerm (apply func (list pair)) fuzz)
  92.           )
  93.         )
  94.         lst
  95.       )
  96.     )
  97.   )
  98.  
  99.   (defun prelst (lst el / f)
  100.     (vl-remove-if
  101.       '(lambda (a) (or f (setq f (equal a el 1e-8))))
  102.       lst
  103.     )
  104.   )
  105.  
  106.   (defun suflst (lst el)
  107.     (cdr (vl-member-if '(lambda (a) (equal a el 1e-8)) lst))
  108.   )
  109.  
  110.   (defun unit (v)
  111.     (mapcar '(lambda (x) (/ x (distance '(0.0 0.0 0.0) v))) v)
  112.   )
  113.  
  114.   (setq
  115.     vl (mapcar
  116.          'cdr
  117.          (vl-remove-if-not
  118.            '(lambda (x) (= (car x) 10))
  119.            (entget pol)
  120.          )
  121.        )
  122.   )
  123.   (setq vl (cons (last vl) vl))
  124.   (setq tl (mapcar '(lambda (a b) (mapcar '- b a)) vl (cdr vl)))
  125.   (setq tl (mapcar '(lambda (x) (unit x)) tl))
  126.   (setq vtl (mapcar '(lambda (a b) (list a b)) vl tl))
  127.   (setq rl (mapcar '(lambda (a b) (ridge a b))
  128.                    tl
  129.                    (cdr (reverse (cons (car tl) (reverse tl))))
  130.            )
  131.   )
  132.   (setq rl (mapcar '(lambda (x) (unit x)) rl))
  133.   (setq vrl (mapcar '(lambda (a b) (list a b))
  134.                     (cdr (reverse (cons (car vl) (reverse vl))))
  135.                     rl
  136.             )
  137.   )
  138.   (setq vrl (cons (last vrl) (reverse (cdr (reverse vrl)))))
  139.   (setq pln T)
  140.  
  141.   (defun kr (lst / pl plnn plnnn pll plll vpx1 vpxp1 vpx2 vpxp2 pp vxx z)
  142.     (mapcar
  143.       '(lambda (a b)
  144.          (setq p
  145.                 (inters (car a)
  146.                         (mapcar '+ (car a) (cadr a))
  147.                         (car b)
  148.                         (mapcar '+ (car b) (cadr b))
  149.                         nil
  150.                 )
  151.          )
  152.          (setq pl (cons p pl))
  153.          (if
  154.            (and
  155.              (vl-catch-all-apply
  156.                'onlin-p
  157.                (list (car a) p (mapcar '+ (car a) (cadr a)))
  158.              )
  159.  
  160.              (vl-catch-all-apply
  161.                'onlin-p
  162.                (list (car b) p (mapcar '+ (car b) (cadr b)))
  163.              )
  164.            )
  165.             (setq pll (cons p pll))
  166.          )
  167.          (if
  168.            (and
  169.              (vl-catch-all-apply
  170.                'onlin-p
  171.                (list (mapcar '+ (car a) (cadr a)) p (car a))
  172.              )
  173.  
  174.              (vl-catch-all-apply
  175.                'onlin-p
  176.                (list (mapcar '+ (car b) (cadr b)) p (car b))
  177.              )
  178.            )
  179.             (setq plll (cons p plll))
  180.          )
  181.        )
  182.       (reverse (cons (car lst) (reverse lst)))
  183.       (cdr (reverse (cons (car lst) (reverse lst))))
  184.     )
  185.     (setq vpl (mapcar '(lambda (a b c) (list (list a b) (list c b)))
  186.                       vl
  187.                       (reverse pl)
  188.                       (cdr vl)
  189.               )
  190.     )
  191.     (setq vpl (apply 'append vpl))
  192.     (while (assocon nil vpl 'cadr 1e-6)
  193.       (setq vpl (vl-remove (assocon nil vpl 'cadr 1e-6) vpl))
  194.     )
  195.     (setq pln nil)
  196.     (foreach p pl
  197.       (if (vl-some
  198.             '(lambda (x)
  199.                (vl-catch-all-apply 'onlin-p (list (car x) (cadr x) p))
  200.              )
  201.             vpl
  202.           )
  203.         (setq pln (cons p pln))
  204.       )
  205.     )
  206.     (foreach p (reverse pln)
  207.       (if (vl-some
  208.             '(lambda (x)
  209.                (vl-catch-all-apply 'onlin-p (list (car x) p (cadr x)))
  210.              )
  211.             vpl
  212.           )
  213.         (setq pln (vl-remove-if '(lambda (x) (equal x p 1e-6)) pln))
  214.       )
  215.     )
  216.     (foreach p pl
  217.       (setq vpx1 (assocon p vpl 'cadr 1e-6))
  218.       (setq vpxp1 (if (last (prelst vpl vpx1))
  219.                     (last (prelst vpl vpx1))
  220.                     (last vpl)
  221.                   )
  222.       )
  223.       (if (equal (car vpx1) (car vpxp1) 1e-6)
  224.         (setq vpxp1 (if (last (prelst vpl vpxp1))
  225.                       (last (prelst vpl vpxp1))
  226.                       (last vpl)
  227.                     )
  228.         )
  229.       )
  230.       (setq vpx2 (assocon p (vl-remove vpx1 vpl) 'cadr 1e-6))
  231.       (setq vpxp2 (if (car (suflst vpl vpx2))
  232.                     (car (suflst vpl vpx2))
  233.                     (car vpl)
  234.                   )
  235.       )
  236.       (if (equal (car vpx2) (car vpxp2) 1e-6)
  237.         (setq vpxp2 (if (car (suflst vpl vpxp2))
  238.                       (car (suflst vpl vpxp2))
  239.                       (car vpl)
  240.                     )
  241.         )
  242.       )
  243.       (if (or (and (equal (unit (mapcar '- (cadr vpx1) (car vpx1)))
  244.                           (unit (mapcar '- (car vpxp1) (cadr vpxp1)))
  245.                           1e-6
  246.                    )
  247.                    (equal (unit (mapcar '- (cadr vpx2) (car vpx2)))
  248.                           (unit (mapcar '- (car vpxp2) (cadr vpxp2)))
  249.                           1e-6
  250.                    )
  251.               )
  252.               (and (equal (unit (mapcar '- (cadr vpx1) (car vpx1)))
  253.                           (unit (mapcar '- (cadr vpxp1) (car vpxp1)))
  254.                           1e-6
  255.                    )
  256.                    (equal (unit (mapcar '- (cadr vpx2) (car vpx2)))
  257.                           (unit (mapcar '- (cadr vpxp2) (car vpxp2)))
  258.                           1e-6
  259.                    )
  260.               )
  261.               (and (equal (unit (mapcar '- (cadr vpx1) (car vpx1)))
  262.                           (unit (mapcar '- (car vpxp1) (cadr vpxp1)))
  263.                           1e-6
  264.                    )
  265.                    (equal (unit (mapcar '- (cadr vpx2) (car vpx2)))
  266.                           (unit (mapcar '- (cadr vpxp2) (car vpxp2)))
  267.                           1e-6
  268.                    )
  269.               )
  270.               (and (equal (unit (mapcar '- (cadr vpx1) (car vpx1)))
  271.                           (unit (mapcar '- (cadr vpxp1) (car vpxp1)))
  272.                           1e-6
  273.                    )
  274.                    (equal (unit (mapcar '- (cadr vpx2) (car vpx2)))
  275.                           (unit (mapcar '- (car vpxp2) (cadr vpxp2)))
  276.                           1e-6
  277.                    )
  278.               )
  279.           )
  280.         (setq plnn (cons p plnn))
  281.       )
  282.     )
  283.     (foreach p plnn
  284.       (if (vl-member-if '(lambda (x) (equal x p 1e-6)) pll)
  285.         (setq plnnn (cons p plnnn))
  286.       )
  287.     )
  288.     (setq pln (append pln plnnn))
  289.     (setq vx nil)
  290.     (foreach p pln
  291.       (mapcar '(lambda (x)
  292.                  (if (equal (cadr x) p 1e-6)
  293.                    (setq vx (cons x vx))
  294.                  )
  295.                )
  296.               vpl
  297.       )
  298.     )
  299.     (foreach p plll
  300.       (mapcar '(lambda (x)
  301.                  (if (equal (cadr x) p 1e-6)
  302.                    (setq vx (vl-remove x vx))
  303.                  )
  304.                )
  305.               vx
  306.       )
  307.     )
  308.     (if (not (= (length pln) 1))
  309.       (setq
  310.         pln (list (cadar
  311.                     (setq vx (vl-sort vx
  312.                                       '(lambda (a b)
  313.                                          (< (distance (car a) (cadr a))
  314.                                             (distance (car b) (cadr b))
  315.                                          )
  316.                                        )
  317.                              )
  318.                     )
  319.                   )
  320.             )
  321.       )
  322.     )
  323.     (setq i 0)
  324.     (if (and vx ppl (not (= (length pln) 1)))
  325.       (while
  326.         (and (if (< (setq i (1+ i)) (length vx))
  327.                T
  328.                (progn (setq pln (list (cadr (nth 0 vx)))) nil)
  329.              )
  330.              (not
  331.                (vl-some
  332.                  '(lambda (x)
  333.                     (equal (list (car x) (cadr x)) (car pln) 1e-6)
  334.                   )
  335.                  (mapcar 'cadr
  336.                          (vl-remove (list (list nil nil) (list nil nil))
  337.                                     (_reml ppl vx)
  338.                          )
  339.                  )
  340.                )
  341.              )
  342.         )
  343.          (setq pln (list (cadr (nth i vx))))
  344.       )
  345.     )
  346.     (if (null vx)
  347.       (setq pln nil)
  348.     )
  349.     (if (vl-every '(lambda (x) (equal (cadar vpl) (cadr x) 1e-6))
  350.                   vpl
  351.         )
  352.       (progn
  353.         (foreach l (unique vpl)
  354.           (if
  355.             (not (vl-some
  356.                    '(lambda (x) (equal (list (car l) (cadr l)) x 1e-6))
  357.                    ppl
  358.                  )
  359.             )
  360.              (setq z  (entmakex (list '(0 . "LINE")
  361.                                       (cons 10 (car l))
  362.                                       (cons 11 (cadr l))
  363.                                 )
  364.                       )
  365.                    zz (cons z zz)
  366.              )
  367.           )
  368.         )
  369.         (setq pln nil)
  370.       )
  371.     )
  372.     (if (and (equal pln (list nil)) (= (length (unique vrl)) 2))
  373.       (if (not
  374.             (vl-some
  375.               '(lambda (x)
  376.                  (equal (list (caar (unique vrl)) (caadr (unique vrl)))
  377.                         x
  378.                         1e-6
  379.                  )
  380.                )
  381.               ppl
  382.             )
  383.           )
  384.         (setq z  (entmakex (list '(0 . "LINE")
  385.                                  (cons 10 (caar (unique vrl)))
  386.                                  (cons 11 (caadr (unique vrl)))
  387.                            )
  388.                  )
  389.               zz (cons z zz)
  390.         )
  391.       )
  392.     )
  393.     (if (equal pln (list nil))
  394.       (setq pln nil)
  395.     )
  396.     (foreach p pln
  397.       (setq vp1 (cons (assocon p vpl 'cadr 1e-6) vp1))
  398.       (setq vp2
  399.              (cons (assocon p (cdr (member (car vp1) vpl)) 'cadr 1e-6)
  400.                    vp2
  401.              )
  402.       )
  403.       (if (car vp1)
  404.         (if
  405.           (not
  406.             (vl-some
  407.               '(lambda (x) (equal (list (caar vp1) (cadar vp1)) x 1e-6))
  408.               ppl
  409.             )
  410.           )
  411.            (setq z  (entmakex (list '(0 . "LINE")
  412.                                     (cons 10 (caar vp1))
  413.                                     (cons 11 (cadar vp1))
  414.                               )
  415.                     )
  416.                  zz (cons z zz)
  417.            )
  418.         )
  419.       )
  420.       (if (car vp2)
  421.         (if
  422.           (not
  423.             (vl-some
  424.               '(lambda (x) (equal (list (caar vp2) (cadar vp2)) x 1e-6))
  425.               ppl
  426.             )
  427.           )
  428.            (setq z  (entmakex (list '(0 . "LINE")
  429.                                     (cons 10 (caar vp2))
  430.                                     (cons 11 (cadar vp2))
  431.                               )
  432.                     )
  433.                  zz (cons z zz)
  434.            )
  435.         )
  436.       )
  437.       (setq vpp2 (caar vp2))
  438.       (setq v2 nil)
  439.       (while (setq vpp2 (car (assocon vpp2 vp2 'cadr 1e-6)))
  440.         (if (not (null vpp2))
  441.           (setq v2 vpp2)
  442.         )
  443.       )
  444.       (if (null v2)
  445.         (setq v2 (caar vp2))
  446.       )
  447.       (setq vpp1 (caar vp1))
  448.       (setq v1 nil)
  449.       (while (setq vpp1 (car (assocon vpp1 vp1 'cadr 1e-6)))
  450.         (if (not (null vpp1))
  451.           (setq v1 vpp1)
  452.         )
  453.       )
  454.       (if (null v1)
  455.         (setq v1 (caar vp1))
  456.       )
  457.       (setq pp
  458.              (list
  459.                p
  460.                (unit
  461.                  (ridge
  462.                    (if
  463.                      (cadr
  464.                        (last (prelst vtl (assocon v1 vtl 'car 1e-6)))
  465.                      )
  466.                       (cadr
  467.                         (last (prelst vtl (assocon v1 vtl 'car 1e-6)))
  468.                       )
  469.                       (cadr (last vtl))
  470.                    )
  471.                    (cadr (assocon v2 vtl 'car 1e-6))
  472.                  )
  473.                )
  474.              )
  475.       )
  476.       (setq vrl (if vrl
  477.                   (subst pp (assocon (caar vp1) vrl 'car 1e-6) vrl)
  478.                   (subst pp (assocon (caar vp1) lst 'car 1e-6) lst)
  479.                 )
  480.             vrl (if (assocon (caar vp2) vrl 'car 1e-6)
  481.                   (subst pp (assocon (caar vp2) vrl 'car 1e-6) vrl)
  482.                 )
  483.       )
  484.       (setq vl (subst p (caar vp1) vl)
  485.             vl (subst p (caar vp2) vl)
  486.       )
  487.     )
  488.   )
  489.  
  490.   (while pln (kr vrl))
  491. )
  492.  
  493. (defun c:2droof-MR (/ *error*)
  494.  
  495.  
  496.   (defun *error* (msg)
  497.     (if zz
  498.       (setq zz nil)
  499.     )
  500.     (if poly
  501.       (setq poly nil)
  502.     )
  503.     )
  504.     (if msg
  505.       (prompt msg)
  506.     )
  507.     (princ)
  508.   )
  509.  
  510.   )
  511.  
  512.   (setq poly
  513.          (car
  514.            (entsel
  515.              "\nPick closed LWPOLYLINE in WCS oriented CCW and with only straight segments-edges"
  516.            )
  517.          )
  518.   )
  519.  
  520.   (2droof poly)
  521.  
  522.   (*error* nil)
  523.  
  524.   (princ)
  525. )
  526.  

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 07, 2013, 06:06:41 AM
Found some bug in final code... Now should be OK...

M.R. :wink:
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: snownut2 on September 07, 2013, 08:14:57 AM
M.R.

Your Ridge-Line function works great, you might want to edit your "Allowed" lwpolyline type to also require that the LWPOLYLINE be closed. 

NICE WORK.... :kewl:
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: CAB on September 07, 2013, 05:45:07 PM
Nice work.

Fails on pline closed when start & end point both have a vertex in the list.
Code: [Select]
(-1 . <Entity name: 78e362b8>)
(0 . "LWPOLYLINE")
(330 . <Entity name: 7ee6fc10>)
(5 . "9D887")
(100 . "AcDbEntity")
(67 . 0)
(410 . "Model")
(8 . "0")
(100 . "AcDbPolyline")
(90 . 7)
(70 . 1)
(43 . 0.0)
(38 . 0.0)
(39 . 0.0)
(10 2257.34 2328.59)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2447.94 2328.59)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2447.94 2181.56)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2335.75 2181.56)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2335.75 2222.18)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2257.34 2222.18)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2257.34 2328.59)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(210 0.0 0.0 1.0)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: CAB on September 07, 2013, 05:49:04 PM
This one is a tough one to solve,
Code: [Select]
(-1 . <Entity name: 78e36248>)
(0 . "LWPOLYLINE")
(330 . <Entity name: 7ee6fc10>)
(5 . "9D879")
(100 . "AcDbEntity")
(67 . 0)
(410 . "Model")
(8 . "0")
(100 . "AcDbPolyline")
(90 . 8)
(70 . 1)
(43 . 0.0)
(38 . 0.0)
(39 . 0.0)
(10 2054.14 2133.5)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2054.14 2184.36)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 1984.3 2184.36)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 1984.3 2328.33)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2201.47 2328.33)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2201.47 2163.63)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2108.51 2163.63)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2108.51 2133.5)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(210 0.0 0.0 1.0)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: CAB on September 07, 2013, 05:58:15 PM
I was surprised your routine did well on this flattened version of a very complex roof.
It did leave one extra line, see red.
Code: [Select]
(-1 . <Entity name: 78e59cf8>)
(0 . "LWPOLYLINE")
(330 . <Entity name: 78e6dc10>)
(5 . "31BF")
(100 . "AcDbEntity")
(67 . 0)
(410 . "Model")
(8 . "0")
(100 . "AcDbPolyline")
(90 . 18)
(70 . 1)
(43 . 0.0)
(38 . 0.0)
(39 . 0.0)
(10 788.057 1041.58)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 926.057 1041.58)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 926.057 883.583)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 1090.06 883.583)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 1090.06 898.583)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 1198.06 898.583)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 1198.06 877.583)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 1410.06 877.583)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 1410.06 1355.58)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 1363.56 1355.58)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 1363.56 1369.58)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 1245.56 1369.58)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 1245.56 1355.58)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 1216.06 1355.58)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 1216.06 1417.58)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 969.266 1417.58)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 969.266 1563.58)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 788.057 1563.58)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(210 0.0 0.0 1.0)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: CAB on September 07, 2013, 06:03:49 PM
Here is another one it has trouble with.
Code: [Select]
(-1 . <Entity name: 78de2858>)
(0 . "LWPOLYLINE")
(330 . <Entity name: 78df7c10>)
(5 . "19F53")
(100 . "AcDbEntity")
(67 . 0)
(410 . "Model")
(8 . "TEXT")
(100 . "AcDbPolyline")
(90 . 10)
(70 . 1)
(43 . 0.0)
(38 . 0.0)
(39 . 0.0)
(10 2666.24 -241.035)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2433.3 -241.035)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2433.3 111.629)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2666.24 111.629)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2666.24 40.2463)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2990.99 40.2461)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2990.99 -238.486)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2868.57 -238.486)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2868.57 -178.151)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(10 2666.24 -178.15)
(40 . 0.0)
(41 . 0.0)
(42 . 0.0)
(210 0.0 0.0 1.0)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 08, 2013, 04:23:45 AM
@ CAB :
Look CAB, yes if you have 2 adjacent vertexes that overlap or start-end vertex overlap, you have to use clean_poly.lsp by gille...
This is explanation for first case...
Second and fourth case that you posted have vertexes created in CW direction... In my code it was explicitly said that vertexes must be CCW... It is assumed that LWPOLYLINE has to be closed also - with "C" - close option...
Your third case it solved correctly, with more than less success... I also said that roof solution must be unique - pline must not have porches that are actually roof dormers - that's why it failed to do it 100% correct...
Also your fourth case and if it was CCW isn't unique single roof solution - you have separate compositions witch are behaving as dormers... So this case also can't be solved with my code 2droof-MR... In this cases it's best to use previously made routine witch uses extrude command (c:roof)...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 09, 2013, 02:15:43 PM
Look, I have trouble with complex plines, so my version maybe isn't so good... I've changed my code and I am posting this version witch will do the job if it's simple solution, otherwise if you wait a little it will blink (erase and make lines) on the place witch is problematical... I'll attach my complex LWPOLYLINE witch can't be solved on this my netbook (I don't have PC at the moment)...

So here is my better blinking version and I strongly suggest that you use this one for now...

Code - Auto/Visual Lisp: [Select]
  1. (defun 2droof (pol  ints   f /      _reml  unique _vl-remove    ridge
  2.                onlin-p       assocon       prelst suflst ll     pl
  3.                pln    i      rl     tl     v      v1     v2     vl
  4.                vp     vp1    vp2    vpp1   vpp2   vpl    vrl    vtl
  5.                vx     p1     p2     pp     ppl    int    lin    p2s
  6.                ss1    sss1   ss2    sss2
  7.               )
  8.  
  9.   (defun _reml (l1 l2 / a n ls)
  10.     (while
  11.       (setq n nil
  12.             a (car l2)
  13.       )
  14.        (while (and l1 (null n))
  15.          (if (equal a (car l1) 1e-8)
  16.            (setq l1 (cdr l1)
  17.                  n  t
  18.            )
  19.            (setq ls (append ls (list (car l1)))
  20.                  l1 (cdr l1)
  21.            )
  22.          )
  23.        )
  24.        (setq l2 (cdr l2))
  25.     )
  26.     (append ls l1)
  27.   )
  28.  
  29.   (defun unique (lst)
  30.     (if lst
  31.       (cons (car lst)
  32.             (unique (_vl-remove (car lst) (cdr lst) 1e-6))
  33.       )
  34.     )
  35.   )
  36.  
  37.   (defun _vl-remove (el lst fuzz)
  38.     (vl-remove-if
  39.       '(lambda (x)
  40.          (and (equal (car x) (car el) fuzz)
  41.               (equal (cadr x) (cadr el) fuzz)
  42.          )
  43.        )
  44.       lst
  45.     )
  46.   )
  47.  
  48.   (defun ridge (v1 v2)
  49.     (if (not
  50.           (minusp (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1))))
  51.         )
  52.       (mapcar '*
  53.               (list -1.0 -1.0 -1.0)
  54.               (mapcar '- v1 v2)
  55.       )
  56.       (if (or (equal (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1)))
  57.                      0.0
  58.                      1e-8
  59.               )
  60.               (equal (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1)))
  61.                      -0.0
  62.                      -1e-8
  63.               )
  64.           )
  65.         (if (equal v1 v2 1e-8)
  66.           (polar '(0.0 0.0 0.0)
  67.                  (+ (angle '(0.0 0.0 0.0) v1) (/ pi 2.0))
  68.                  1.0
  69.           )
  70.           v2
  71.         )
  72.         (mapcar '- v1 v2)
  73.       )
  74.     )
  75.   )
  76.  
  77.   (defun onlin-p (p1 p2 p)
  78.     (and
  79.       (equal (distance p1 p2)
  80.              (+ (distance p1 p) (distance p2 p))
  81.              1e-6
  82.       )
  83.       (not (equal (distance p1 p) 0.0 1e-6))
  84.       (not (equal (distance p2 p) 0.0 1e-6))
  85.     )
  86.   )
  87.  
  88.   (defun assocon (SearchTerm Lst func fuzz)
  89.     (car
  90.       (vl-member-if
  91.         (function
  92.           (lambda (pair)
  93.             (equal SearchTerm (apply func (list pair)) fuzz)
  94.           )
  95.         )
  96.         lst
  97.       )
  98.     )
  99.   )
  100.  
  101.   (defun prelst (lst el / f)
  102.     (vl-remove-if
  103.       '(lambda (a) (or f (setq f (equal a el 1e-8))))
  104.       lst
  105.     )
  106.   )
  107.  
  108.   (defun suflst (lst el)
  109.     (cdr (vl-member-if '(lambda (a) (equal a el 1e-8)) lst))
  110.   )
  111.  
  112.   (defun unit (v)
  113.     (mapcar '(lambda (x) (/ x (distance '(0.0 0.0 0.0) v))) v)
  114.   )
  115.  
  116.   (setq ppl nil)
  117.   (if zz
  118.     (foreach z zz
  119.       (setq p1  (cdr (assoc 10 (entget z)))
  120.             p2  (cdr (assoc 11 (entget z)))
  121.             pp  (list (list (car p1) (cadr p1)) (list (car p2) (cadr p2)))
  122.             ppl (cons pp ppl)
  123.       )
  124.     )
  125.   )
  126.   (if ppl
  127.     (setq ppl (reverse ppl))
  128.   )
  129.   (setq
  130.     vl (mapcar
  131.          'cdr
  132.          (vl-remove-if-not
  133.            '(lambda (x) (= (car x) 10))
  134.            (entget pol)
  135.          )
  136.        )
  137.   )
  138.   (foreach tf f
  139.     (setq vl (reverse (cdr (reverse (cons (last vl) vl)))))
  140.   )
  141.   (setq vl (cons (last vl) vl))
  142.   (setq tl (mapcar '(lambda (a b) (mapcar '- b a)) vl (cdr vl)))
  143.   (setq tl (mapcar '(lambda (x) (unit x)) tl))
  144.   (setq vtl (mapcar '(lambda (a b) (list a b)) vl tl))
  145.   (setq rl (mapcar '(lambda (a b) (ridge a b))
  146.                    tl
  147.                    (cdr (reverse (cons (car tl) (reverse tl))))
  148.            )
  149.   )
  150.   (setq rl (mapcar '(lambda (x) (unit x)) rl))
  151.   (setq vrl (mapcar '(lambda (a b) (list a b))
  152.                     (cdr (reverse (cons (car vl) (reverse vl))))
  153.                     rl
  154.             )
  155.   )
  156.   (setq vrl (cons (last vrl) (reverse (cdr (reverse vrl)))))
  157.   (setq pln T)
  158.  
  159.   (defun kr (lst / pl plnn plnnn pll plll vpx1 vpxp1 vpx2 vpxp2 pp vxx z)
  160.     (mapcar
  161.       '(lambda (a b)
  162.          (setq p
  163.                 (inters (car a)
  164.                         (mapcar '+ (car a) (cadr a))
  165.                         (car b)
  166.                         (mapcar '+ (car b) (cadr b))
  167.                         nil
  168.                 )
  169.          )
  170.          (setq pl (cons p pl))
  171.          (if
  172.            (and
  173.              (vl-catch-all-apply
  174.                'onlin-p
  175.                (list (car a) p (mapcar '+ (car a) (cadr a)))
  176.              )
  177.  
  178.              (vl-catch-all-apply
  179.                'onlin-p
  180.                (list (car b) p (mapcar '+ (car b) (cadr b)))
  181.              )
  182.            )
  183.             (setq pll (cons p pll))
  184.          )
  185.          (if
  186.            (and
  187.              (vl-catch-all-apply
  188.                'onlin-p
  189.                (list (mapcar '+ (car a) (cadr a)) p (car a))
  190.              )
  191.  
  192.              (vl-catch-all-apply
  193.                'onlin-p
  194.                (list (mapcar '+ (car b) (cadr b)) p (car b))
  195.              )
  196.            )
  197.             (setq plll (cons p plll))
  198.          )
  199.        )
  200.       (reverse (cons (car lst) (reverse lst)))
  201.       (cdr (reverse (cons (car lst) (reverse lst))))
  202.     )
  203.     (setq vpl (mapcar '(lambda (a b c) (list (list a b) (list c b)))
  204.                       vl
  205.                       (reverse pl)
  206.                       (cdr vl)
  207.               )
  208.     )
  209.     (setq vpl (apply 'append vpl))
  210.     (while (assocon nil vpl 'cadr 1e-6)
  211.       (setq vpl (vl-remove (assocon nil vpl 'cadr 1e-6) vpl))
  212.     )
  213.     (setq pln nil)
  214.     (foreach p pl
  215.       (if (vl-some
  216.             '(lambda (x)
  217.                (vl-catch-all-apply 'onlin-p (list (car x) (cadr x) p))
  218.              )
  219.             vpl
  220.           )
  221.         (setq pln (cons p pln))
  222.       )
  223.     )
  224.     (foreach p (reverse pln)
  225.       (if (vl-some
  226.             '(lambda (x)
  227.                (vl-catch-all-apply 'onlin-p (list (car x) p (cadr x)))
  228.              )
  229.             vpl
  230.           )
  231.         (setq pln (vl-remove-if '(lambda (x) (equal x p 1e-6)) pln))
  232.       )
  233.     )
  234.     (foreach p pl
  235.       (setq vpx1 (assocon p vpl 'cadr 1e-6))
  236.       (setq vpxp1 (if (last (prelst vpl vpx1))
  237.                     (last (prelst vpl vpx1))
  238.                     (last vpl)
  239.                   )
  240.       )
  241.       (if (equal (car vpx1) (car vpxp1) 1e-6)
  242.         (setq vpxp1 (if (last (prelst vpl vpxp1))
  243.                       (last (prelst vpl vpxp1))
  244.                       (last vpl)
  245.                     )
  246.         )
  247.       )
  248.       (setq vpx2 (assocon p (vl-remove vpx1 vpl) 'cadr 1e-6))
  249.       (setq vpxp2 (if (car (suflst vpl vpx2))
  250.                     (car (suflst vpl vpx2))
  251.                     (car vpl)
  252.                   )
  253.       )
  254.       (if (equal (car vpx2) (car vpxp2) 1e-6)
  255.         (setq vpxp2 (if (car (suflst vpl vpxp2))
  256.                       (car (suflst vpl vpxp2))
  257.                       (car vpl)
  258.                     )
  259.         )
  260.       )
  261.       (if (or (and (equal (unit (mapcar '- (cadr vpx1) (car vpx1)))
  262.                           (unit (mapcar '- (car vpxp1) (cadr vpxp1)))
  263.                           1e-6
  264.                    )
  265.                    (equal (unit (mapcar '- (cadr vpx2) (car vpx2)))
  266.                           (unit (mapcar '- (car vpxp2) (cadr vpxp2)))
  267.                           1e-6
  268.                    )
  269.               )
  270.               (and (equal (unit (mapcar '- (cadr vpx1) (car vpx1)))
  271.                           (unit (mapcar '- (cadr vpxp1) (car vpxp1)))
  272.                           1e-6
  273.                    )
  274.                    (equal (unit (mapcar '- (cadr vpx2) (car vpx2)))
  275.                           (unit (mapcar '- (cadr vpxp2) (car vpxp2)))
  276.                           1e-6
  277.                    )
  278.               )
  279.               (and (equal (unit (mapcar '- (cadr vpx1) (car vpx1)))
  280.                           (unit (mapcar '- (car vpxp1) (cadr vpxp1)))
  281.                           1e-6
  282.                    )
  283.                    (equal (unit (mapcar '- (cadr vpx2) (car vpx2)))
  284.                           (unit (mapcar '- (cadr vpxp2) (car vpxp2)))
  285.                           1e-6
  286.                    )
  287.               )
  288.               (and (equal (unit (mapcar '- (cadr vpx1) (car vpx1)))
  289.                           (unit (mapcar '- (cadr vpxp1) (car vpxp1)))
  290.                           1e-6
  291.                    )
  292.                    (equal (unit (mapcar '- (cadr vpx2) (car vpx2)))
  293.                           (unit (mapcar '- (car vpxp2) (cadr vpxp2)))
  294.                           1e-6
  295.                    )
  296.               )
  297.           )
  298.         (setq plnn (cons p plnn))
  299.       )
  300.     )
  301.     (foreach p plnn
  302.       (if (vl-member-if '(lambda (x) (equal x p 1e-6)) pll)
  303.         (setq plnnn (cons p plnnn))
  304.       )
  305.     )
  306.     (setq pln (append pln plnnn))
  307.     (setq vx nil)
  308.     (foreach p pln
  309.       (mapcar '(lambda (x)
  310.                  (if (equal (cadr x) p 1e-6)
  311.                    (setq vx (cons x vx))
  312.                  )
  313.                )
  314.               vpl
  315.       )
  316.     )
  317.     (foreach p plll
  318.       (mapcar '(lambda (x)
  319.                  (if (equal (cadr x) p 1e-6)
  320.                    (setq vx (vl-remove x vx))
  321.                  )
  322.                )
  323.               vx
  324.       )
  325.     )
  326.     (foreach tf f
  327.       (setq ints (reverse (cdr (reverse (cons (last ints) ints)))))
  328.     )
  329.     (setq f nil)
  330.     (if ints
  331.       (foreach v vx
  332.         (if (vl-some '(lambda (x) (equal x (cadr v) 1e-6)) ints)
  333.           (setq pln (list (cadr v)))
  334.         )
  335.       )
  336.     )
  337.     (if (and ints (not (= (length pln) 1)))
  338.       (foreach v vpl
  339.         (if (vl-some '(lambda (x) (equal x (cadr v) 1e-6)) ints)
  340.           (setq pln (list (cadr v)))
  341.         )
  342.       )
  343.     )
  344.     (if (not (= (length pln) 1))
  345.       (setq
  346.         pln (list (cadar
  347.                     (setq vx (vl-sort vx
  348.                                       '(lambda (a b)
  349.                                          (< (distance (car a) (cadr a))
  350.                                             (distance (car b) (cadr b))
  351.                                          )
  352.                                        )
  353.                              )
  354.                     )
  355.                   )
  356.             )
  357.       )
  358.     )
  359.     (setq i 0)
  360.     (if (and vx ppl (not (= (length pln) 1)))
  361.       (while
  362.         (and (if (< (setq i (1+ i)) (length vx))
  363.                T
  364.                (progn (setq pln (list (cadr (nth 0 vx)))) nil)
  365.              )
  366.              (not
  367.                (vl-some
  368.                  '(lambda (x)
  369.                     (equal (list (car x) (cadr x)) (car pln) 1e-6)
  370.                   )
  371.                  (mapcar 'cadr (vl-remove (list (list nil nil) (list nil nil)) (_reml ppl vx)))
  372.                )
  373.              )
  374.         )
  375.          (setq pln (list (cadr (nth i vx))))
  376.       )
  377.     )
  378.     (if (null vx)
  379.       (setq pln nil)
  380.     )
  381.     (if (vl-every '(lambda (x) (equal (cadar vpl) (cadr x) 1e-6))
  382.                   vpl
  383.         )
  384.       (progn
  385.         (foreach l (unique vpl)
  386.           (if
  387.             (not (vl-some
  388.                    '(lambda (x) (equal (list (car l) (cadr l)) x 1e-6))
  389.                    ppl
  390.                  )
  391.             )
  392.              (setq z  (entmakex (list '(0 . "LINE")
  393.                                       (cons 10 (car l))
  394.                                       (cons 11 (cadr l))
  395.                                 )
  396.                       )
  397.                    zz (cons z zz)
  398.              )
  399.           )
  400.         )
  401.         (setq pln nil)
  402.       )
  403.     )
  404.     (if (and (equal pln (list nil)) (= (length (unique vrl)) 2))
  405.       (if (not
  406.             (vl-some
  407.               '(lambda (x)
  408.                  (equal (list (caar (unique vrl)) (caadr (unique vrl)))
  409.                         x
  410.                         1e-6
  411.                  )
  412.                )
  413.               ppl
  414.             )
  415.           )
  416.         (setq z  (entmakex (list '(0 . "LINE")
  417.                                  (cons 10 (caar (unique vrl)))
  418.                                  (cons 11 (caadr (unique vrl)))
  419.                            )
  420.                  )
  421.               zz (cons z zz)
  422.         )
  423.       )
  424.     )
  425.     (if (equal pln (list nil))
  426.       (setq pln nil)
  427.     )
  428.     (foreach p pln
  429.       (setq vp1 (cons (assocon p vpl 'cadr 1e-6) vp1))
  430.       (setq vp2
  431.              (cons (assocon p (cdr (member (car vp1) vpl)) 'cadr 1e-6)
  432.                    vp2
  433.              )
  434.       )
  435.       (if (car vp1)
  436.         (if
  437.           (not
  438.             (vl-some
  439.               '(lambda (x) (equal (list (caar vp1) (cadar vp1)) x 1e-6))
  440.               ppl
  441.             )
  442.           )
  443.            (setq z  (entmakex (list '(0 . "LINE")
  444.                                     (cons 10 (caar vp1))
  445.                                     (cons 11 (cadar vp1))
  446.                               )
  447.                     )
  448.                  zz (cons z zz)
  449.            )
  450.         )
  451.       )
  452.       (if (car vp2)
  453.         (if
  454.           (not
  455.             (vl-some
  456.               '(lambda (x) (equal (list (caar vp2) (cadar vp2)) x 1e-6))
  457.               ppl
  458.             )
  459.           )
  460.            (setq z  (entmakex (list '(0 . "LINE")
  461.                                     (cons 10 (caar vp2))
  462.                                     (cons 11 (cadar vp2))
  463.                               )
  464.                     )
  465.                  zz (cons z zz)
  466.            )
  467.         )
  468.       )
  469.       (setq vpp2 (caar vp2))
  470.       (setq v2 nil)
  471.       (while (setq vpp2 (car (assocon vpp2 vp2 'cadr 1e-6)))
  472.         (if (not (null vpp2))
  473.           (setq v2 vpp2)
  474.         )
  475.       )
  476.       (if (null v2)
  477.         (setq v2 (caar vp2))
  478.       )
  479.       (setq vpp1 (caar vp1))
  480.       (setq v1 nil)
  481.       (while (setq vpp1 (car (assocon vpp1 vp1 'cadr 1e-6)))
  482.         (if (not (null vpp1))
  483.           (setq v1 vpp1)
  484.         )
  485.       )
  486.       (if (null v1)
  487.         (setq v1 (caar vp1))
  488.       )
  489.       (setq pp
  490.              (list
  491.                p
  492.                (unit
  493.                  (ridge
  494.                    (if
  495.                      (cadr
  496.                        (last (prelst vtl (assocon v1 vtl 'car 1e-6)))
  497.                      )
  498.                       (cadr
  499.                         (last (prelst vtl (assocon v1 vtl 'car 1e-6)))
  500.                       )
  501.                       (cadr (last vtl))
  502.                    )
  503.                    (cadr (assocon v2 vtl 'car 1e-6))
  504.                  )
  505.                )
  506.              )
  507.       )
  508.       (setq vrl (if vrl
  509.                   (subst pp (assocon (caar vp1) vrl 'car 1e-6) vrl)
  510.                   (subst pp (assocon (caar vp1) lst 'car 1e-6) lst)
  511.                 )
  512.             vrl (if (assocon (caar vp2) vrl 'car 1e-6)
  513.                   (subst pp (assocon (caar vp2) vrl 'car 1e-6) vrl)
  514.                 )
  515.       )
  516.       (setq vl (subst p (caar vp1) vl)
  517.             vl (subst p (caar vp2) vl)
  518.       )
  519.     )
  520.   )
  521.  
  522.   (while pln (kr vrl))
  523.  
  524.   (setq ints nil)
  525.   (setq vl nil)
  526.   (setq ppl nil)
  527.   (foreach z zz
  528.     (setq p1  (cdr (assoc 10 (entget z)))
  529.           p2  (cdr (assoc 11 (entget z)))
  530.           pp  (list (list (car p1) (cadr p1)) (list (car p2) (cadr p2)))
  531.           ppl (cons pp ppl)
  532.     )
  533.   )
  534.   (setq ppl (reverse ppl))
  535.   (foreach lin1 ppl
  536.     (foreach lin2 (vl-remove lin1 ppl)
  537.       (if (and (not (equal (car lin1) (list nil nil)))
  538.                (not (equal (cadr lin1) (list nil nil)))
  539.                (not (equal (car lin2) (list nil nil)))
  540.                (not (equal (cadr lin2) (list nil nil)))
  541.                (setq int
  542.                       (inters (car lin1) (cadr lin1) (car lin2) (cadr lin2))
  543.                )
  544.                (not (equal int (car lin1) 1e-6))
  545.                (not (equal int (cadr lin1) 1e-6))
  546.                (not (equal int (car lin2) 1e-6))
  547.                (not (equal int (cadr lin2) 1e-6))
  548.           )
  549.         (setq ints (cons int ints))
  550.       )
  551.     )
  552.   )
  553.   (foreach int ints
  554.     (setq ss1 (ssget "_C" int int))
  555.     (setq sss1 (acet-ss-union (list ss1 sss1)))
  556.   )
  557.   (if ints
  558.     (progn
  559.       (setq i -1)
  560.       (while (setq lin (ssname sss1 (setq i (1+ i))))
  561.         (setq p1 (cdr (assoc 10 (entget lin)))
  562.               p2 (cdr (assoc 11 (entget lin)))
  563.               pp (list (list (car p1) (cadr p1)) (list (car p2) (cadr p2)))
  564.         )
  565.         (entdel lin)
  566.         (setq zz (vl-remove (nth (vl-position pp ppl) zz) zz))
  567.         (setq p2s (cons p2 p2s))
  568.         (setq ppl nil)
  569.         (foreach z zz
  570.           (setq p1  (cdr (assoc 10 (entget z)))
  571.                 p2  (cdr (assoc 11 (entget z)))
  572.                 pp  (list (list (car p1) (cadr p1)) (list (car p2) (cadr p2)))
  573.                 ppl (cons pp ppl)
  574.           )
  575.         )
  576.         (setq ppl (reverse ppl))
  577.       )
  578.       (gc)
  579.       (foreach p2 (acet-list-remove-duplicates p2s 1e-6)
  580.         (setq ss2 (ssget "_C" p2 p2))
  581.         (setq sss2 (acet-ss-union (list ss2 sss2)))
  582.       )
  583.       (setq i -1)
  584.       (while (setq lin (ssname sss2 (setq i (1+ i))))
  585.         (setq p1 (cdr (assoc 10 (entget lin)))
  586.               p2 (cdr (assoc 11 (entget lin)))
  587.               pp (list (list (car p1) (cadr p1)) (list (car p2) (cadr p2)))
  588.         )
  589.         (entdel lin)
  590.         (setq zz (vl-remove (nth (vl-position pp ppl) zz) zz))
  591.         (setq ppl nil)
  592.         (foreach z zz
  593.           (setq p1  (cdr (assoc 10 (entget z)))
  594.                 p2  (cdr (assoc 11 (entget z)))
  595.                 pp  (list (list (car p1) (cadr p1)) (list (car p2) (cadr p2)))
  596.                 ppl (cons pp ppl)
  597.           )
  598.         )
  599.         (setq ppl (reverse ppl))
  600.       )
  601.       (setq ff (cons T ff))
  602.       (setq f ff)
  603.       (2droof poly (acet-list-remove-duplicates ints 1e-6) f)
  604.     )
  605.   )
  606. )
  607.  
  608. (defun c:2droof-MR (/ *error*)
  609.  
  610.  
  611.   (defun *error* (msg)
  612.     (if f
  613.       (setq f nil)
  614.     )
  615.     (if ff
  616.       (setq ff nil)
  617.     )
  618.     (if zz
  619.       (setq zz nil)
  620.     )
  621.     (if poly
  622.       (setq poly nil)
  623.     )
  624.     )
  625.     (if msg
  626.       (prompt msg)
  627.     )
  628.     (princ)
  629.   )
  630.  
  631.   )
  632.  
  633.   (setq poly
  634.          (car
  635.            (entsel
  636.              "\nPick closed LWPOLYLINE in WCS oriented CCW and with only straight segments-edges"
  637.            )
  638.          )
  639.   )
  640.  
  641.   (2droof poly nil nil)
  642.  
  643.   (*error* nil)
  644.  
  645.   (princ)
  646. )
  647.  

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: krampaul82 on September 09, 2013, 02:32:59 PM
Cab Wrote
LeeMac has invested considerable time and effort in learning to code well. I think he has every right
to recoup something for his investment.  He has shared his talent freely in many threads so I feel
he has given plenty to our community.

Krampaul82 wrote;
Agreed!
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 10, 2013, 08:16:15 AM
Cab Wrote
LeeMac has invested considerable time and effort in learning to code well. I think he has every right
to recoup something for his investment.  He has shared his talent freely in many threads so I feel
he has given plenty to our community.

Krampaul82 wrote;
Agreed!

I agree with the fact that Lee has every right to recoup, only if his code proves that it's worth... Beside that, in this particular case, my codes that I provided have their purpose to help someone learn something... Although they are not perfect, I believe that Lee said that his version also isn't 100% perfect... Even the code witch uses built-in extrude command can't in all situations do the job... This is difficult task and my engagement was predominantly to help... So that's all, my final - not last blinking code changed to finish execution with all lines that may intersect each other (with blinking version, you can't know when to hit ESC to capture the most solved result)...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 06, 2014, 03:54:21 AM
I've changed algorithm and I think this is better till now (from me)... Still there are some lacks, but my example posted above solved successfully...

Code: [Select]
(defun 2droof (pol    /      insidep       unique _vl-remove    ridge  onlin-p       assocon       prelst suflst
               ll     pl     pln    i      rl     tl     v      v1     v2     vl     vp     vp1    vp2    vpp1   vpp2   vpl    vrl    vtl    vx
              )

  (defun insidep (pt entn / big flag obj1 obj2 obj3 p1 p2 small)
    (if (and pt entn)
      (progn
        (setq obj1 (vlax-ename->vla-object entn))
        (setq obj2 (car (vlax-invoke obj1 'Offset 0.001))
              obj3 (car (vlax-invoke obj1 'Offset -0.001))
        )
        (if (> (vla-get-area obj2) (vla-get-area obj3))
          (progn
            (set 'big obj2)
            (set 'small obj3)
          )
          (progn
            (set 'big obj3)
            (set 'small obj2)
          )
        )
        (setq p1 (vlax-curve-getClosestPointTo big pt)
              p2 (vlax-curve-getClosestPointTo small pt)
        )
        (if (> (distance pt p1) (distance pt p2))
          (setq flag T)
          (setq flag nil)
        )
        (mapcar (function (lambda (x)
                            (progn
                              (vla-delete x)
                              (vlax-release-object x)
                            )
                          )
                )
                (list big small)
        )
      )
    )
    flag
  )

  (defun unique (lst)
    (if lst
      (cons (car lst)
            (unique (_vl-remove (car lst) (cdr lst) 1e-6))
      )
    )
  )

  (defun _vl-remove (el lst fuzz)
    (vl-remove-if
      '(lambda (x)
         (and (equal (car x) (car el) fuzz)
              (equal (cadr x) (cadr el) fuzz)
         )
       )
      lst
    )
  )

  (defun ridge (v1 v2)
    (if (not
          (minusp (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1))))
        )
      (mapcar '*
              (list -1.0 -1.0 -1.0)
              (mapcar '- v1 v2)
      )
      (if (or (equal (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1)))
                     0.0
                     1e-8
              )
              (equal (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1)))
                     -0.0
                     -1e-8
              )
          )
        (if (equal v1 v2 1e-8)
          (polar '(0.0 0.0 0.0)
                 (+ (angle '(0.0 0.0 0.0) v1) (/ pi 2.0))
                 1.0
          )
          v2
        )
        (mapcar '- v1 v2)
      )
    )
  )

  (defun onlin-p (p1 p2 p)
    (and
      (equal (distance p1 p2)
             (+ (distance p1 p) (distance p2 p))
             1e-8
      )
      (not (equal p1 p 1e-8))
      (not (equal p2 p 1e-8))
    )
  )

  (defun assocon (SearchTerm Lst func fuzz)
    (car
      (vl-member-if
        (function
          (lambda (pair)
            (equal SearchTerm (apply func (list pair)) fuzz)
          )
        )
        lst
      )
    )
  )

  (defun prelst (lst el / f)
    (vl-remove-if
      '(lambda (a) (or f (setq f (equal a el 1e-8))))
      lst
    )
  )

  (defun suflst (lst el)
    (cdr (vl-member-if '(lambda (a) (equal a el 1e-8)) lst))
  )

  (defun unit (v)
    (mapcar '(lambda (x) (/ x (distance '(0.0 0.0 0.0) v))) v)
  )

  (setq
    vl (mapcar
         'cdr
         (vl-remove-if-not
           '(lambda (x) (= (car x) 10))
           (entget pol)
         )
       )
  )
  (setq vl (cons (last vl) vl))
  (setq tl (mapcar '(lambda (a b) (mapcar '- b a)) vl (cdr vl)))
  (setq tl (mapcar '(lambda (x) (unit x)) tl))
  (setq vtl (mapcar '(lambda (a b) (list a b)) vl tl))
  (setq rl (mapcar '(lambda (a b) (ridge a b))
                   tl
                   (cdr (reverse (cons (car tl) (reverse tl))))
           )
  )
  (setq rl (mapcar '(lambda (x) (unit x)) rl))
  (setq vrl (mapcar '(lambda (a b) (list a b))
                    (cdr (reverse (cons (car vl) (reverse vl))))
                    rl
            )
  )
  (setq vrl (cons (last vrl) (reverse (cdr (reverse vrl)))))
  (setq pln T)

  (defun kr (lst / pl vpx1 vpxp1 vpx2 vpxp2 pp vxx z)
    (mapcar
      '(lambda (a b)
         (setq p
                (inters (car a)
                        (mapcar '+ (car a) (cadr a))
                        (car b)
                        (mapcar '+ (car b) (cadr b))
                        nil
                )
         )
         (setq pl (cons p pl))
       )
      (reverse (cons (car lst) (reverse lst)))
      (cdr (reverse (cons (car lst) (reverse lst))))
    )
    (setq vpl (mapcar '(lambda (a b c) (list (list a b) (list c b)))
                      vl
                      (reverse pl)
                      (cdr (reverse (cons (car vl) (reverse vl))))
              )
    )
    (setq vpl (apply 'append vpl))
;    (while (assocon nil vpl 'cadr 1e-6)
;      (setq vpl (vl-remove (assocon nil vpl 'cadr 1e-6) vpl))
;    )
    (setq pln nil)
    (foreach p pl
      (if (vl-some
            '(lambda (x)
               (vl-catch-all-apply 'onlin-p (list (car x) (cadr x) p))
             )
            vpl
          )
        (setq pln (cons p pln))
      )
    )
    (setq pln (mapcar '(lambda (x) (if (insidep x pol) x)) pln))
    (setq pln (vl-remove nil pln))
    (setq vx nil)
    (foreach p pln
      (mapcar '(lambda (x)
                 (if (equal (cadr x) p 1e-6)
                   (setq vx (cons x vx))
                 )
               )
              vpl
      )
    )
    (setq vxx vx)
    (if plnz (setq plnzz (cons (car plnz) plnzz)))
    (if (and (not (= (length pln) 1)) vx)
      (progn
        (setq vx (vl-sort vx
                          '(lambda (a b)
                             (< (distance (car a) (cadr a))
                                (distance (car b) (cadr b))
                             )
                           )
                 )
        )
        (if plnz
          (progn
            (setq vx (vl-sort vx
                              '(lambda (a b)
                                 (< (distance (cadr a) (car plnz))
                                    (distance (cadr b) (car plnz))
                                 )
                               )
                     )
            )
            (setq vx (vl-remove-if-not '(lambda (x) (equal (car x) (car plnz) 1e-6)) vx))
            (if (and (eq (length vx) 2)
                     (> (distance (caar vx) (cadar vx))
                        (distance (caadr vx) (cadadr vx))
                     )
                )
              (setq vx (cons (cadr vx) (cons (car vx) (cddr vx))))
            )
            (foreach v vpl
              (if (setq catch (vl-catch-all-apply
                                'onlin-p
                                (list (car v) (cadr v) (car plnz))
                              )
                  )
                (if (vl-catch-all-error-p catch)
                      (setq tst (cons nil tst))
                      (setq tst (cons T tst))
                )
                (setq tst (cons nil tst))
              )
            )
            (setq vxxx (vl-sort vxx
                          '(lambda (a b)
                             (< (distance (car a) (cadr a))
                                (distance (car b) (cadr b))
                             )
                           )
                       )
            )
            (if plnzz
              (foreach p plnzz
                (if (assocon p vxxx 'car 1e-6)
                  (setq
                    vxxx (vl-remove (assocon p vxxx 'car 1e-6) vxxx)
                  )
                )
                (if (assocon p vxxx 'car 1e-6)
                  (setq
                    vxxx (vl-remove (assocon p vxxx 'car 1e-6) vxxx)
                  )
                )
              )
            )
            (if (or (/= (length vx) 2)
                    (eval (cons 'or tst))
                    (if (and vxxx vx (onlin-p (caar vx) (cadar vxxx) (cadar vx)))
                      (> (distance (caar vx) (cadar vx))
                         (distance (caar vx) (cadar vxxx))
                      )
                      (if (not (null vxxx))
                        (> (distance (caar vx) (cadar vx))
                           (distance (caar vxxx) (cadar vxxx))
                        )
                        nil
                      )
                    )
                )
              (progn
                (if vxxx
                  (setq vx vxxx)
                  (setq vx (vl-sort vxx
                          '(lambda (a b)
                             (< (distance (car a) (cadr a))
                                (distance (car b) (cadr b))
                             )
                           )
                       )
                  )
                )
              )
            )
          )
        )
        (setq pln (list (cadar vx)))
      )
    )
    (setq tst nil)
    (if (null vx)
      (setq pln nil)
    )
    (if (vl-every '(lambda (x) (equal (cadar vpl) (cadr x) 1e-6))
                  vpl
        )
      (if (and (vl-every '(lambda (x) (eq (cadr x) nil)) vpl)
               (eq (length (unique vpl)) 2)
          )
        (progn
          (setq z  (entmakex (list '(0 . "LINE")
                                   (cons 10 (caar (unique vpl)))
                                   (cons 11 (caadr (unique vpl)))
                             )
                   )
                zz (cons z zz)
          )
          (setq pln nil)
        )
        (progn
          (foreach l (unique vpl)
            (setq z  (entmakex (list '(0 . "LINE")
                                     (cons 10 (car l))
                                     (cons 11 (cadr l))
                               )
                     )
                  zz (cons z zz)
            )
          )
          (setq pln nil)
        )
      )
    )
    (if (equal pln (list nil))
      (setq pln nil)
    )
    (setq plnz pln)
    (foreach p pln
      (setq vp1 (cons (assocon p vpl 'cadr 1e-6) vp1))
      (setq vp2 (cons (assocon p (cdr (member (car vp1) vpl)) 'cadr 1e-6) vp2))
      (if (car vp1)
        (setq z  (entmakex (list '(0 . "LINE")
                                 (cons 10 (caar vp1))
                                 (cons 11 (cadar vp1))
                           )
                 )
              zz (cons z zz)
        )
      )
      (if (car vp2)
        (setq z  (entmakex (list '(0 . "LINE")
                                 (cons 10 (caar vp2))
                                 (cons 11 (cadar vp2))
                           )
                 )
              zz (cons z zz)
        )
      )
      (setq vpp2 (caar vp2))
      (setq v2 nil)
      (while (setq vpp2 (car (assocon vpp2 vp2 'cadr 1e-6)))
        (if (not (null vpp2))
          (setq v2 vpp2)
        )
      )
      (if (null v2)
        (setq v2 (caar vp2))
      )
      (setq vpp1 (caar vp1))
      (setq v1 nil)
      (while (setq vpp1 (car (assocon vpp1 vp1 'cadr 1e-6)))
        (if (not (null vpp1))
          (setq v1 vpp1)
        )
      )
      (if (null v1)
        (setq v1 (caar vp1))
      )
      (setq pp
             (list
               p
               (unit
                 (ridge
                   (if
                     (cadr
                       (last (prelst vtl (assocon v1 vtl 'car 1e-6)))
                     )
                      (cadr
                        (last (prelst vtl (assocon v1 vtl 'car 1e-6)))
                      )
                      (cadr (last vtl))
                   )
                   (cadr (assocon v2 vtl 'car 1e-6))
                 )
               )
             )
      )
      (setq vrl (if vrl
                  (subst pp (assocon (caar vp1) vrl 'car 1e-6) vrl)
                  (subst pp (assocon (caar vp1) lst 'car 1e-6) lst)
                )
            vrl (if (assocon (caar vp2) vrl 'car 1e-6)
                  (subst pp (assocon (caar vp2) vrl 'car 1e-6) vrl)
                )
      )
      (setq vl (subst p (caar vp1) vl)
            vl (subst p (caar vp2) vl)
      )
    )
  )

  (while pln (kr vrl))
)

(defun c:2droof-MR (/ *error* adoc plnz plnzz)

  (vl-load-com)
  (setq adoc (vla-get-activedocument (vlax-get-acad-object)))

  (defun *error* (msg)
    (if plnz
      (setq plnz nil)
    )
    (if plnzz
      (setq plnzz nil)
    )
    (if zz
      (setq zz nil)
    )
    (if poly
      (setq poly nil)
    )
    (vla-endundomark adoc)
    (if msg
      (prompt msg)
    )
    (princ)
  )

  (vla-startundomark adoc)

  (setq poly
         (car
           (entsel
             "\nPick closed LWPOLYLINE in WCS oriented CCW and with only straight segments-edges"
           )
         )
  )

  (2droof poly)

  (*error* nil)

  (princ)
)

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: pedroantonio on February 06, 2014, 04:41:43 AM
Nice job ribarm
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: GP on February 06, 2014, 10:06:24 AM
My version.  :-)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 06, 2014, 10:41:38 AM
Thank you Gian... I was wondering when I'll get some support... Your code works fantastic...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: Fabricio28 on February 06, 2014, 10:45:46 AM
Thank you Gian...  Your code works fantastic...

x2
 Fantastic!  :-D
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 06, 2014, 06:54:46 PM
There is more food for thoughts, but this one is the worst... But, perhaps someone find it usable in making good mods...

Code: [Select]
(defun 2droof (pol    /      insidep       unique _vl-remove    ridge  onlin-p       assocon       prelst suflst
               ll     pl     pln    i      rl     tl     v      v1     v2     vl     vp     vp1    vp2    vpp1   vpp2   vpl    vrl    vtl    vx     fl
              )

  (defun insidep (pt entn / big flag obj1 obj2 obj3 p1 p2 small)
    (if (and pt entn)
      (progn
        (setq obj1 (vlax-ename->vla-object entn))
        (setq obj2 (car (vlax-invoke obj1 'Offset 0.001))
              obj3 (car (vlax-invoke obj1 'Offset -0.001))
        )
        (if (> (vla-get-area obj2) (vla-get-area obj3))
          (progn
            (set 'big obj2)
            (set 'small obj3)
          )
          (progn
            (set 'big obj3)
            (set 'small obj2)
          )
        )
        (setq p1 (vlax-curve-getClosestPointTo big pt)
              p2 (vlax-curve-getClosestPointTo small pt)
        )
        (if (> (distance pt p1) (distance pt p2))
          (setq flag T)
          (setq flag nil)
        )
        (mapcar (function (lambda (x)
                            (progn
                              (vla-delete x)
                              (vlax-release-object x)
                            )
                          )
                )
                (list big small)
        )
      )
    )
    flag
  )

  (defun unique (lst)
    (if lst
      (cons (car lst)
            (unique (_vl-remove (car lst) (cdr lst) 1e-6))
      )
    )
  )

  (defun _vl-remove (el lst fuzz)
    (vl-remove-if
      '(lambda (x)
         (and (equal (car x) (car el) fuzz)
              (equal (cadr x) (cadr el) fuzz)
         )
       )
      lst
    )
  )

  (defun ridge (v1 v2)
    (if (not
          (minusp (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1))))
        )
      (mapcar '*
              (list -1.0 -1.0 -1.0)
              (mapcar '- v1 v2)
      )
      (if (or (equal (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1)))
                     0.0
                     1e-8
              )
              (equal (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1)))
                     -0.0
                     -1e-8
              )
          )
        (if (equal v1 v2 1e-8)
          (polar '(0.0 0.0 0.0)
                 (+ (angle '(0.0 0.0 0.0) v1) (/ pi 2.0))
                 1.0
          )
          v2
        )
        (mapcar '- v1 v2)
      )
    )
  )

  (defun onlin-p (p1 p2 p)
    (and
      (equal (distance p1 p2)
             (+ (distance p1 p) (distance p2 p))
             1e-8
      )
      (not (equal p1 p 1e-8))
      (not (equal p2 p 1e-8))
    )
  )

  (defun assocon (SearchTerm Lst func fuzz)
    (car
      (vl-member-if
        (function
          (lambda (pair)
            (equal SearchTerm (apply func (list pair)) fuzz)
          )
        )
        lst
      )
    )
  )

  (defun prelst (lst el / f)
    (vl-remove-if
      '(lambda (a) (or f (setq f (equal a el 1e-8))))
      lst
    )
  )

  (defun suflst (lst el)
    (cdr (vl-member-if '(lambda (a) (equal a el 1e-8)) lst))
  )

  (defun unit (v)
    (mapcar '(lambda (x) (/ x (distance '(0.0 0.0 0.0) v))) v)
  )

  (setq
    vl (mapcar
         'cdr
         (vl-remove-if-not
           '(lambda (x) (= (car x) 10))
           (entget pol)
         )
       )
  )
  (setq vl (cons (last vl) vl))
  (setq tl (mapcar '(lambda (a b) (mapcar '- b a)) vl (cdr vl)))
  (setq tl (mapcar '(lambda (x) (unit x)) tl))
  (setq vtl (mapcar '(lambda (a b) (list a b)) vl tl))
  (setq rl (mapcar '(lambda (a b) (ridge a b))
                   tl
                   (cdr (reverse (cons (car tl) (reverse tl))))
           )
  )
  (setq rl (mapcar '(lambda (x) (unit x)) rl))
  (setq vrl (mapcar '(lambda (a b) (list a b))
                    (cdr (reverse (cons (car vl) (reverse vl))))
                    rl
            )
  )
  (setq vrl (cons (last vrl) (reverse (cdr (reverse vrl)))))
  (setq pln T)

  (defun kr (lst / pl vpx1 vpxp1 vpx2 vpxp2 pp vxx z)
    (mapcar
      '(lambda (a b)
         (setq p
                (inters (car a)
                        (mapcar '+ (car a) (cadr a))
                        (car b)
                        (mapcar '+ (car b) (cadr b))
                        nil
                )
         )
         (setq pl (cons p pl))
       )
      (reverse (cons (car lst) (reverse lst)))
      (cdr (reverse (cons (car lst) (reverse lst))))
    )
    (setq vpl (mapcar '(lambda (a b c) (list (list a b) (list c b)))
                      vl
                      (reverse pl)
                      (cdr (reverse (cons (car vl) (reverse vl))))
              )
    )
    (setq vpl (apply 'append vpl))
;    (while (assocon nil vpl 'cadr 1e-6)
;      (setq vpl (vl-remove (assocon nil vpl 'cadr 1e-6) vpl))
;    )
    (setq pln nil)
    (foreach p pl
      (if (vl-some
            '(lambda (x)
               (vl-catch-all-apply 'onlin-p (list (car x) (cadr x) p))
             )
            vpl
          )
        (setq pln (cons p pln))
      )
    )
    (setq pln (mapcar '(lambda (x) (if (insidep x pol) x)) pln))
    (setq pln (vl-remove nil pln))
    (setq vx nil)
    (foreach p pln
      (mapcar '(lambda (x)
                 (if (equal (cadr x) p 1e-6)
                   (setq vx (cons x vx))
                 )
               )
              vpl
      )
    )
    (if (and (null fl) (null plnn)) (setq plnn pln fl t))
    (setq vxx vx)
    (if plnz (setq plnzz (cons (car plnz) plnzz)))
    (if (and (not (= (length pln) 1)) vx)
      (progn
        (setq vx (vl-sort vx
                          '(lambda (a b)
                             (< (distance (car a) (cadr a))
                                (distance (car b) (cadr b))
                             )
                           )
                 )
        )
        (if (car plnn)
          (if (vl-some
                '(lambda (x)
                   (onlin-p (car x) (cadr x) (car plnn))
                 )
                vx
              )
            (if (assocon (car plnn) vx 'cadr 1e-6)
              (setq
                vx (subst (assocon (car plnn) vx 'cadr 1e-6) (car vx) vx)
              )
            )
          )
        )
        (if (and (null plnn) plnz)
          (progn
            (setq vx (vl-sort vx
                              '(lambda (a b)
                                 (< (distance (cadr a) (car plnz))
                                    (distance (cadr b) (car plnz))
                                 )
                               )
                     )
            )
            (setq vx (vl-remove-if-not '(lambda (x) (equal (car x) (car plnz) 1e-6)) vx))
            (if (and (eq (length vx) 2)
                     (> (distance (caar vx) (cadar vx))
                        (distance (caadr vx) (cadadr vx))
                     )
                )
              (setq vx (cons (cadr vx) (cons (car vx) (cddr vx))))
            )
            (foreach v vpl
              (if (setq catch (vl-catch-all-apply
                                'onlin-p
                                (list (car v) (cadr v) (car plnz))
                              )
                  )
                (if (vl-catch-all-error-p catch)
                      (setq tst (cons nil tst))
                      (setq tst (cons T tst))
                )
                (setq tst (cons nil tst))
              )
            )
            (setq vxxx (vl-sort vxx
                          '(lambda (a b)
                             (< (distance (car a) (cadr a))
                                (distance (car b) (cadr b))
                             )
                           )
                       )
            )
            (if plnzz
              (foreach p plnzz
                (if (assocon p vxxx 'car 1e-6)
                  (setq
                    vxxx (vl-remove (assocon p vxxx 'car 1e-6) vxxx)
                  )
                )
                (if (assocon p vxxx 'car 1e-6)
                  (setq
                    vxxx (vl-remove (assocon p vxxx 'car 1e-6) vxxx)
                  )
                )
              )
            )
            (if (or (/= (length vx) 2)
                    (eval (cons 'or tst))
                    (if (and vxxx vx (onlin-p (caar vx) (cadar vxxx) (cadar vx)))
                      (> (distance (caar vx) (cadar vx))
                         (distance (caar vx) (cadar vxxx))
                      )
                      (if (not (null vxxx))
                        (> (distance (caar vx) (cadar vx))
                           (distance (caar vxxx) (cadar vxxx))
                        )
                        nil
                      )
                    )
                )
              (progn
                (if vxxx
                  (setq vx (vl-sort vxxx
                            '(lambda (a b)
                               (< (distance (cadr a) (car plnz))
                                  (distance (cadr b) (car plnz))
                               )
                             )
                           )
                  )
                  (setq vx (vl-sort vxx
                            '(lambda (a b)
                               (< (distance (car a) (cadr a))
                                  (distance (car b) (cadr b))
                               )
                             )
                           )
                  )
                )
              )
            )
          )
        )
        (setq plnn (cdr plnn))
        (setq pln (list (cadar vx)))
      )
    )
    (setq tst nil)
    (if (null vx)
      (setq pln nil)
    )
    (if (vl-every '(lambda (x) (equal (cadar vpl) (cadr x) 1e-6))
                  vpl
        )
      (if (and (vl-every '(lambda (x) (eq (cadr x) nil)) vpl)
               (eq (length (unique vpl)) 2)
          )
        (progn
          (setq z  (entmakex (list '(0 . "LINE")
                                   (cons 10 (caar (unique vpl)))
                                   (cons 11 (caadr (unique vpl)))
                             )
                   )
                zz (cons z zz)
          )
          (setq pln nil)
        )
        (progn
          (foreach l (unique vpl)
            (setq z  (entmakex (list '(0 . "LINE")
                                     (cons 10 (car l))
                                     (cons 11 (cadr l))
                               )
                     )
                  zz (cons z zz)
            )
          )
          (setq pln nil)
        )
      )
    )
    (if (equal pln (list nil))
      (setq pln nil)
    )
    (setq plnz pln)
    (foreach p pln
      (setq vp1 (cons (assocon p vpl 'cadr 1e-6) vp1))
      (setq vp2 (cons (assocon p (cdr (member (car vp1) vpl)) 'cadr 1e-6) vp2))
      (if (car vp1)
        (setq z  (entmakex (list '(0 . "LINE")
                                 (cons 10 (caar vp1))
                                 (cons 11 (cadar vp1))
                           )
                 )
              zz (cons z zz)
        )
      )
      (if (car vp2)
        (setq z  (entmakex (list '(0 . "LINE")
                                 (cons 10 (caar vp2))
                                 (cons 11 (cadar vp2))
                           )
                 )
              zz (cons z zz)
        )
      )
      (setq vpp2 (caar vp2))
      (setq v2 nil)
      (while (setq vpp2 (car (assocon vpp2 vp2 'cadr 1e-6)))
        (if (not (null vpp2))
          (setq v2 vpp2)
        )
      )
      (if (null v2)
        (setq v2 (caar vp2))
      )
      (setq vpp1 (caar vp1))
      (setq v1 nil)
      (while (setq vpp1 (car (assocon vpp1 vp1 'cadr 1e-6)))
        (if (not (null vpp1))
          (setq v1 vpp1)
        )
      )
      (if (null v1)
        (setq v1 (caar vp1))
      )
      (setq pp
             (list
               p
               (unit
                 (ridge
                   (if
                     (cadr
                       (last (prelst vtl (assocon v1 vtl 'car 1e-6)))
                     )
                      (cadr
                        (last (prelst vtl (assocon v1 vtl 'car 1e-6)))
                      )
                      (cadr (last vtl))
                   )
                   (cadr (assocon v2 vtl 'car 1e-6))
                 )
               )
             )
      )
      (setq vrl (if vrl
                  (subst pp (assocon (caar vp1) vrl 'car 1e-6) vrl)
                  (subst pp (assocon (caar vp1) lst 'car 1e-6) lst)
                )
            vrl (if (assocon (caar vp2) vrl 'car 1e-6)
                  (subst pp (assocon (caar vp2) vrl 'car 1e-6) vrl)
                )
      )
      (setq vl (subst p (caar vp1) vl)
            vl (subst p (caar vp2) vl)
      )
    )
  )

  (while pln (kr vrl))
)

(defun c:2droof-MR (/ *error* adoc plnn plnz plnzz)

  (vl-load-com)
  (setq adoc (vla-get-activedocument (vlax-get-acad-object)))

  (defun *error* (msg)
    (if plnn
      (setq plnn nil)
    )
    (if plnz
      (setq plnz nil)
    )
    (if plnzz
      (setq plnzz nil)
    )
    (if zz
      (setq zz nil)
    )
    (if poly
      (setq poly nil)
    )
    (vla-endundomark adoc)
    (if msg
      (prompt msg)
    )
    (princ)
  )

  (vla-startundomark adoc)

  (setq poly
         (car
           (entsel
             "\nPick closed LWPOLYLINE in WCS oriented CCW and with only straight segments-edges"
           )
         )
  )

  (2droof poly)

  (*error* nil)

  (princ)
)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 07, 2014, 06:30:28 AM
Gian Paolo, what ab this one... If it works for you, then please help me... I believe this one is final test...

Please download attachment example...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ElpanovEvgeniy on February 07, 2014, 07:26:27 AM
There is more food for thoughts, but this one is the worst... But, perhaps someone find it usable in making good mods...

Code: [Select]
...

Code: [Select]
(entmakex '((0 . "LWPOLYLINE")
            (100 . "AcDbEntity")
            (410 . "Model")
            (100 . "AcDbPolyline")
            (90 . 10)
            (70 . 1)
            (43 . 0.0)
            (38 . 0.0)
            (39 . 0.0)
            (10 763.88 532.697)
            (10 963.376 567.524)
            (10 886.098 759.253)
            (10 566.259 721.203)
            (10 269.828 475.846)
            (10 63.224 475.846)
            (10 141.075 248.441)
            (10 446.489 368.128)
            (10 446.489 113.793)
            (10 763.88 113.793)
            (210 0.0 0.0 1.0)
           )
)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 07, 2014, 08:06:06 AM
There is more food for thoughts, but this one is the worst... But, perhaps someone find it usable in making good mods...

Code: [Select]
...

Code: [Select]
(entmakex '((0 . "LWPOLYLINE")
            (100 . "AcDbEntity")
            (410 . "Model")
            (100 . "AcDbPolyline")
            (90 . 10)
            (70 . 1)
            (43 . 0.0)
            (38 . 0.0)
            (39 . 0.0)
            (10 763.88 532.697)
            (10 963.376 567.524)
            (10 886.098 759.253)
            (10 566.259 721.203)
            (10 269.828 475.846)
            (10 63.224 475.846)
            (10 141.075 248.441)
            (10 446.489 368.128)
            (10 446.489 113.793)
            (10 763.88 113.793)
            (210 0.0 0.0 1.0)
           )
)

Evgeniy, you used my last code that was wrong... And my previous posted in [ code ][/ code ] tags isn't also much better as I thought... You should return to my earlier posts... Test it with this one :

Code: [Select]
(defun 2droof (pol / insidep _reml  unique _vl-remove    ridge  onlin-p
               assocon       prelst suflst ll     pl     pln    i
               rl     tl     v      v1     v2     vl     vp     vp1
               vp2    vpp1   vpp2   vpl    vrl    vtl    vx
              )

  (defun insidep (pt entn / big flag obj1 obj2 obj3 p1 p2 small)
    (if (and pt entn)
      (progn
        (setq obj1 (vlax-ename->vla-object entn))
        (setq obj2 (car (vlax-invoke obj1 'Offset 0.001))
              obj3 (car (vlax-invoke obj1 'Offset -0.001))
        )
        (if (> (vla-get-area obj2) (vla-get-area obj3))
          (progn
            (set 'big obj2)
            (set 'small obj3)
          )
          (progn
            (set 'big obj3)
            (set 'small obj2)
          )
        )
        (setq p1 (vlax-curve-getClosestPointTo big pt)
              p2 (vlax-curve-getClosestPointTo small pt)
        )
        (if (> (distance pt p1) (distance pt p2))
          (setq flag T)
          (setq flag nil)
        )
        (mapcar (function (lambda (x)
                            (progn
                              (vla-delete x)
                              (vlax-release-object x)
                            )
                          )
                )
                (list big small)
        )
      )
    )
    flag
  )

  (defun _reml (l1 l2 / a n ls)
    (while
      (setq n nil
            a (car l2)
      )
       (while (and l1 (null n))
         (if (equal a (car l1) 1e-8)
           (setq l1 (cdr l1)
                 n  t
           )
           (setq ls (append ls (list (car l1)))
                 l1 (cdr l1)
           )
         )
       )
       (setq l2 (cdr l2))
    )
    (append ls l1)
  )

  (defun unique (lst)
    (if lst
      (cons (car lst)
            (unique (_vl-remove (car lst) (cdr lst) 1e-6))
      )
    )
  )

  (defun _vl-remove (el lst fuzz)
    (vl-remove-if
      '(lambda (x)
         (and (equal (car x) (car el) fuzz)
              (equal (cadr x) (cadr el) fuzz)
         )
       )
      lst
    )
  )

  (defun ridge (v1 v2)
    (if (not
          (minusp (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1))))
        )
      (mapcar '*
              (list -1.0 -1.0 -1.0)
              (mapcar '- v1 v2)
      )
      (if (or (equal (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1)))
                     0.0
                     1e-8
              )
              (equal (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1)))
                     -0.0
                     -1e-8
              )
          )
        (if (equal v1 v2 1e-8)
          (polar '(0.0 0.0 0.0)
                 (+ (angle '(0.0 0.0 0.0) v1) (/ pi 2.0))
                 1.0
          )
          v2
        )
        (mapcar '- v1 v2)
      )
    )
  )

  (defun onlin-p (p1 p2 p)
    (and
      (equal (distance p1 p2)
             (+ (distance p1 p) (distance p2 p))
             1e-6
      )
      (not (equal (distance p1 p) 0.0 1e-6))
      (not (equal (distance p2 p) 0.0 1e-6))
    )
  )

  (defun assocon (SearchTerm Lst func fuzz)
    (car
      (vl-member-if
        (function
          (lambda (pair)
            (equal SearchTerm (apply func (list pair)) fuzz)
          )
        )
        lst
      )
    )
  )

  (defun prelst (lst el / f)
    (vl-remove-if
      '(lambda (a) (or f (setq f (equal a el 1e-8))))
      lst
    )
  )

  (defun suflst (lst el)
    (cdr (vl-member-if '(lambda (a) (equal a el 1e-8)) lst))
  )

  (defun unit (v)
    (mapcar '(lambda (x) (/ x (distance '(0.0 0.0 0.0) v))) v)
  )

  (setq
    vl (mapcar
         'cdr
         (vl-remove-if-not
           '(lambda (x) (= (car x) 10))
           (entget pol)
         )
       )
  )
  (setq vl (cons (last vl) vl))
  (setq tl (mapcar '(lambda (a b) (mapcar '- b a)) vl (cdr vl)))
  (setq tl (mapcar '(lambda (x) (unit x)) tl))
  (setq vtl (mapcar '(lambda (a b) (list a b)) vl tl))
  (setq rl (mapcar '(lambda (a b) (ridge a b))
                   tl
                   (cdr (reverse (cons (car tl) (reverse tl))))
           )
  )
  (setq rl (mapcar '(lambda (x) (unit x)) rl))
  (setq vrl (mapcar '(lambda (a b) (list a b))
                    (cdr (reverse (cons (car vl) (reverse vl))))
                    rl
            )
  )
  (setq vrl (cons (last vrl) (reverse (cdr (reverse vrl)))))
  (setq pln T)

  (defun kr (lst / pl plnn plnnn pll plll vpx1 vpxp1 vpx2 vpxp2 pp vxx z)
    (mapcar
      '(lambda (a b)
         (setq p
                (inters (car a)
                        (mapcar '+ (car a) (cadr a))
                        (car b)
                        (mapcar '+ (car b) (cadr b))
                        nil
                )
         )
         (setq pl (cons p pl))
         (if
           (and
             (vl-catch-all-apply
               'onlin-p
               (list (car a) p (mapcar '+ (car a) (cadr a)))
             )

             (vl-catch-all-apply
               'onlin-p
               (list (car b) p (mapcar '+ (car b) (cadr b)))
             )
           )
            (setq pll (cons p pll))
         )
         (if
           (and
             (vl-catch-all-apply
               'onlin-p
               (list (mapcar '+ (car a) (cadr a)) p (car a))
             )

             (vl-catch-all-apply
               'onlin-p
               (list (mapcar '+ (car b) (cadr b)) p (car b))
             )
           )
            (setq plll (cons p plll))
         )
       )
      (reverse (cons (car lst) (reverse lst)))
      (cdr (reverse (cons (car lst) (reverse lst))))
    )
    (setq vpl (mapcar '(lambda (a b c) (list (list a b) (list c b)))
                      vl
                      (reverse pl)
                      (cdr (reverse (cons (car vl) (reverse vl))))
              )
    )
    (setq vpl (apply 'append vpl))
    (while (assocon nil vpl 'cadr 1e-6)
      (setq vpl (vl-remove (assocon nil vpl 'cadr 1e-6) vpl))
    )
    (setq pln nil)
    (foreach p pl
      (if (vl-some
            '(lambda (x)
               (vl-catch-all-apply 'onlin-p (list (car x) (cadr x) p))
             )
            vpl
          )
        (setq pln (cons p pln))
      )
    )
    (foreach p (reverse pln)
      (if (vl-some
            '(lambda (x)
               (vl-catch-all-apply 'onlin-p (list (car x) p (cadr x)))
             )
            vpl
          )
        (setq pln (vl-remove-if '(lambda (x) (equal x p 1e-6)) pln))
      )
    )
    (foreach p pl
      (setq vpx1 (assocon p vpl 'cadr 1e-6))
      (setq vpxp1 (if (last (prelst vpl vpx1))
                    (last (prelst vpl vpx1))
                    (last vpl)
                  )
      )
      (if (equal (car vpx1) (car vpxp1) 1e-6)
        (setq vpxp1 (if (last (prelst vpl vpxp1))
                      (last (prelst vpl vpxp1))
                      (last vpl)
                    )
        )
      )
      (setq vpx2 (assocon p (vl-remove vpx1 vpl) 'cadr 1e-6))
      (setq vpxp2 (if (car (suflst vpl vpx2))
                    (car (suflst vpl vpx2))
                    (car vpl)
                  )
      )
      (if (equal (car vpx2) (car vpxp2) 1e-6)
        (setq vpxp2 (if (car (suflst vpl vpxp2))
                      (car (suflst vpl vpxp2))
                      (car vpl)
                    )
        )
      )
      (if (or (and (equal (unit (mapcar '- (cadr vpx1) (car vpx1)))
                          (unit (mapcar '- (car vpxp1) (cadr vpxp1)))
                          1e-6
                   )
                   (equal (unit (mapcar '- (cadr vpx2) (car vpx2)))
                          (unit (mapcar '- (car vpxp2) (cadr vpxp2)))
                          1e-6
                   )
              )
              (and (equal (unit (mapcar '- (cadr vpx1) (car vpx1)))
                          (unit (mapcar '- (cadr vpxp1) (car vpxp1)))
                          1e-6
                   )
                   (equal (unit (mapcar '- (cadr vpx2) (car vpx2)))
                          (unit (mapcar '- (cadr vpxp2) (car vpxp2)))
                          1e-6
                   )
              )
              (and (equal (unit (mapcar '- (cadr vpx1) (car vpx1)))
                          (unit (mapcar '- (car vpxp1) (cadr vpxp1)))
                          1e-6
                   )
                   (equal (unit (mapcar '- (cadr vpx2) (car vpx2)))
                          (unit (mapcar '- (cadr vpxp2) (car vpxp2)))
                          1e-6
                   )
              )
              (and (equal (unit (mapcar '- (cadr vpx1) (car vpx1)))
                          (unit (mapcar '- (cadr vpxp1) (car vpxp1)))
                          1e-6
                   )
                   (equal (unit (mapcar '- (cadr vpx2) (car vpx2)))
                          (unit (mapcar '- (car vpxp2) (cadr vpxp2)))
                          1e-6
                   )
              )
          )
        (setq plnn (cons p plnn))
      )
    )
    (foreach p plnn
      (if (vl-member-if '(lambda (x) (equal x p 1e-6)) pll)
        (setq plnnn (cons p plnnn))
      )
    )
    (setq pln (append pln plnnn))
    (setq pln (mapcar '(lambda (x) (if (insidep x pol) x)) pln))
    (setq pln (vl-remove nil pln))
    (setq vx nil)
    (foreach p pln
      (mapcar '(lambda (x)
                 (if (equal (cadr x) p 1e-6)
                   (setq vx (cons x vx))
                 )
               )
              vpl
      )
    )
    (foreach p plll
      (mapcar '(lambda (x)
                 (if (equal (cadr x) p 1e-6)
                   (setq vx (vl-remove x vx))
                 )
               )
              vx
      )
    )
    (if (not (= (length pln) 1))
      (setq
        pln (list (cadar
                    (setq vx (vl-sort vx
                                      '(lambda (a b)
                                         (< (distance (car a) (cadr a))
                                            (distance (car b) (cadr b))
                                         )
                                       )
                             )
                    )
                  )
            )
      )
    )
    (setq i 0)
    (if (and vx ppl (not (= (length pln) 1)))
      (while
        (and (if (< (setq i (1+ i)) (length vx))
               T
               (progn (setq pln (list (cadr (nth 0 vx)))) nil)
             )
             (not
               (vl-some
                 '(lambda (x)
                    (equal (list (car x) (cadr x)) (car pln) 1e-6)
                  )
                 (mapcar 'cadr
                         (vl-remove (list (list nil nil) (list nil nil))
                                    (_reml ppl vx)
                         )
                 )
               )
             )
        )
         (setq pln (list (cadr (nth i vx))))
      )
    )
    (if (null vx)
      (setq pln nil)
    )
    (if (vl-every '(lambda (x) (equal (cadar vpl) (cadr x) 1e-6))
                  vpl
        )
      (progn
        (foreach l (unique vpl)
          (if
            (not (vl-some
                   '(lambda (x) (equal (list (car l) (cadr l)) x 1e-6))
                   ppl
                 )
            )
             (setq z  (entmakex (list '(0 . "LINE")
                                      (cons 10 (car l))
                                      (cons 11 (cadr l))
                                )
                      )
                   zz (cons z zz)
             )
          )
        )
        (setq pln nil)
      )
    )
    (if (and (equal pln (list nil)) (= (length (unique vrl)) 2))
      (if (not
            (vl-some
              '(lambda (x)
                 (equal (list (caar (unique vrl)) (caadr (unique vrl)))
                        x
                        1e-6
                 )
               )
              ppl
            )
          )
        (setq z  (entmakex (list '(0 . "LINE")
                                 (cons 10 (caar (unique vrl)))
                                 (cons 11 (caadr (unique vrl)))
                           )
                 )
              zz (cons z zz)
        )
      )
    )
    (if (equal pln (list nil))
      (setq pln nil)
    )
    (foreach p pln
      (setq vp1 (cons (assocon p vpl 'cadr 1e-6) vp1))
      (setq vp2
             (cons (assocon p (cdr (member (car vp1) vpl)) 'cadr 1e-6)
                   vp2
             )
      )
      (if (car vp1)
        (if
          (not
            (vl-some
              '(lambda (x) (equal (list (caar vp1) (cadar vp1)) x 1e-6))
              ppl
            )
          )
           (setq z  (entmakex (list '(0 . "LINE")
                                    (cons 10 (caar vp1))
                                    (cons 11 (cadar vp1))
                              )
                    )
                 zz (cons z zz)
           )
        )
      )
      (if (car vp2)
        (if
          (not
            (vl-some
              '(lambda (x) (equal (list (caar vp2) (cadar vp2)) x 1e-6))
              ppl
            )
          )
           (setq z  (entmakex (list '(0 . "LINE")
                                    (cons 10 (caar vp2))
                                    (cons 11 (cadar vp2))
                              )
                    )
                 zz (cons z zz)
           )
        )
      )
      (setq vpp2 (caar vp2))
      (setq v2 nil)
      (while (setq vpp2 (car (assocon vpp2 vp2 'cadr 1e-6)))
        (if (not (null vpp2))
          (setq v2 vpp2)
        )
      )
      (if (null v2)
        (setq v2 (caar vp2))
      )
      (setq vpp1 (caar vp1))
      (setq v1 nil)
      (while (setq vpp1 (car (assocon vpp1 vp1 'cadr 1e-6)))
        (if (not (null vpp1))
          (setq v1 vpp1)
        )
      )
      (if (null v1)
        (setq v1 (caar vp1))
      )
      (setq pp
             (list
               p
               (unit
                 (ridge
                   (if
                     (cadr
                       (last (prelst vtl (assocon v1 vtl 'car 1e-6)))
                     )
                      (cadr
                        (last (prelst vtl (assocon v1 vtl 'car 1e-6)))
                      )
                      (cadr (last vtl))
                   )
                   (cadr (assocon v2 vtl 'car 1e-6))
                 )
               )
             )
      )
      (setq vrl (if vrl
                  (subst pp (assocon (caar vp1) vrl 'car 1e-6) vrl)
                  (subst pp (assocon (caar vp1) lst 'car 1e-6) lst)
                )
            vrl (if (assocon (caar vp2) vrl 'car 1e-6)
                  (subst pp (assocon (caar vp2) vrl 'car 1e-6) vrl)
                )
      )
      (setq vl (subst p (caar vp1) vl)
            vl (subst p (caar vp2) vl)
      )
    )
  )

  (while pln (kr vrl))
)

(defun c:2droof-MR (/ *error* adoc)

  (vl-load-com)
  (setq adoc (vla-get-activedocument (vlax-get-acad-object)))

  (defun *error* (msg)
    (if zz
      (setq zz nil)
    )
    (if poly
      (setq poly nil)
    )
    (vla-endundomark adoc)
    (if msg
      (prompt msg)
    )
    (princ)
  )

  (vla-startundomark adoc)

  (setq poly
         (car
           (entsel
             "\nPick closed LWPOLYLINE in WCS oriented CCW and with only straight segments-edges"
           )
         )
  )

  (2droof poly)

  (*error* nil)

  (princ)
)

M.R.

But what ab my example I attached... Can you help...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: GP on February 07, 2014, 08:11:44 AM
Thank you Gian... I was wondering when I'll get some support... Your code works fantastic...

x2
 Fantastic!  :-D

Thanks.  :-)


Gian Paolo, what ab this one... If it works for you, then please help me...

Marko, is the roof of your house?  :-D

It does not work for all polylines.
The program checks that there is not a modeling error after SOLIDEDIT command (function "verify_n").


Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 07, 2014, 05:11:41 PM
Do you happen to know why this kind of errors occur... I've modeled roof under this type of polyline and it is correct solution 100% slope and it is totally possible solution... Actually no matter what type of shape is base this kind of modeling of 3D object is always possible especially when it's an ordinary closed 2d polyline with straight edges... So what is the bug here - seems that ACAD can't always be consistent and from your posted picture it's visible that 2d solution of ridge edges isn't problem - CAD does this correctly (but again I think not always)... The bug is encircled piece from your picture and it's the product of modeling operation which is wrong... Look at my attached DWG; I modeled it differently than CAD - find 2d solution, then make polylines with BPOLY and then normal EXTRUDE which is then SLICEd and at the end these all pieces have been UNIONed into single 3D object... It seems that CAD here have serious bug... Just what seemed to be problem for CAD isn't - 2D solution of ridge edges, and what seems to be pretty simple process making from 2D solution 3D object failed... Really weird...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: GP on February 08, 2014, 05:56:14 AM
I had already noticed this problem in the test when I was writing the code.
It occurs randomly on particular polylines, I think it's a bug of  SOLIDEDIT.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 08, 2014, 07:52:14 AM
GP, I've modified your code just a little... I used to model 3D roof according to slope angle, so I've added this to have these 2 options available when 3D mode is choosen... Also I've put (vl-load-com) inside main code as I implemented it into my Startup Suite (I don't want (vl-load-com) to be automatically initialized - of course this is possible only in lower A versions which I happen to poses them on my netbook)... This helps me to better understand functions that are (vl-load-com) independent and if possible I always tend to make code firstly Vanilla varsions of code if possible and only if it's neccessity then I use VLISP... So this is my version (your lisp) that I autoload...

Hope you don't mind I post it...

M.R.

[EDIT : Code updated finally]
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 08, 2014, 03:45:14 PM
I think I solved this problem with modeling operation failures... It was that fact that ACAD creates additional 3DSOLIDs after command SOLIDEDIT "face" "move"... So I didn't realize that GP didn't analyzed my roof.lsp routine thorough and deeply... I've solved earlier how to get rid of additional 3DSOLIDs so that only valid 3DSOLID solution remains...

So my previously attached hr.lsp reattached with this mods... I hope that now it'll not fail in any situation that may occur... If that's the case, please inform here with problematic POLYLINE... Hope that this will be my final post ab this topic, unless someone finds good 2D solution code that may be used in learning purposes like I posted my codes, but none of them isn't 100% reliable...

Regards, M.R. and enjoy with GP's hr.lsp
 :wink:
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 09, 2014, 03:08:46 AM
It seems that it was again me who firstly found wrong POLYLINE... Now I don't know what to do with this kind of entity (POLYLINE)... It seems that we seriously have to find alternative method for this problem...

See attached DWG... Simply unbelievable...
M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 09, 2014, 08:34:33 AM
Like I described to GP what method I used to construct 3D roof from 2D solution, I've decided to automate this process and I succeeded... The result is somewhat modified GP's code (mainly his subfunctions used) - 2droof3d.lsp

HTH, M.R.

So now back to the job... Task Challenge - Find 2D solution of picked LWPOLYLINE...

[EDIT : During some of my tests, I've noticed that 2droof3d.lsp sometimes fails if in line (command "_.ucs" "_3p") haven't been provided points without "_non" preference... So I've added this small issue into these 2 lines... There were total of 13 downloads till I reattached 2sroof3d.lsp]

[EDIT : Testing failed in some situations... Old routine is now 2droof3d-old.lsp (previously named 2droof3d.lsp) and new one is 2droof3d.lsp - new version ask explicitly that LWPOLYLINE must be CCW oriented otherwise reverse it and retry...]

[EDIT : I've found where was my mistake - it was sub-function ListCloskwise-p and that's now fixed... Previous version is now 2droof3d-new-old.lsp (previously named 2droof3d.lsp) and new one is 2droof3d.lsp - new version doesn't need LWPOLYLINE oriented in CCW - it should work in both cases...]

Sorry for inconvenience. M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 11, 2014, 05:29:00 AM
I worked just a little more ab this 2D solutions and yesterday I've made this code... It is with approximately good results, also not 100% reliable, but I must warn you - it is the slowest code I ever made... So if you have example that's not to complex, and soultion is unique - no additional roof porches and you have a time to experiment you can try it... But it's not 100% correct also... My recomendations is to use last code I posted to Evgeniy, if you're not satisfied then try first code posted in [ code ] tags - not [ code=cadlisp-7 ] tags, and if you again aren't satisfied and if you have time and strong computer only then try this code... My motives for posting this one is that fact that it introduces few new sub-functions that may be useful and are related to this task... So I warned you - this isn't the worst code, but certainly the slowest one... Be careful with your time - maybe someone can do it manually faster...

Code: [Select]
(defun 2droof (pol / ListDupes valid-inscribed-circle-p inscribed-circle insidep unique _vl-remove ridge onlin-p assocon prelst suflst ll pl pln i rl tl v v1 v2 vl vp vp1 vp2 vpp1 vpp2 vpl vrl vtl vx)

  (defun ListDupes (l / x r)
    (while l
      (if (vl-member-if '(lambda (a) (equal a (setq x (car l)) 1e-10)) (cdr l))
        (setq r (cons x r))
      )
      (setq l (vl-remove x (cdr l)))
    )
    (reverse (acet-list-remove-duplicates r 1e-6))
  )

  (defun valid-inscribed-circle-p (p pol / plst xl xll ip ipp dstl)
    (setq
      plst (mapcar '(lambda (x) (list (car x) (cadr x) 0.0))
                   (mapcar 'cdr (acet-list-m-assoc 10 (entget pol)))
           )
    )
    (mapcar '(lambda (a b)
               (progn (setq xl
                             (entmakex (list '(0 . "XLINE") '(100 . "AcDbEntity") '(100 . "AcDbXline") (cons 10 a) (cons 11 (unit (mapcar '- b a)))))
                      )
                      (setq xll (cons xl xll))
               )
             )
            plst
            (cdr (reverse (cons (car plst) (reverse plst))))
    )
    (foreach x xll
      (setq ip (vlax-curve-getclosestpointto x p))
      (setq ipp (cons ip ipp))
    )
    (setq dstl (mapcar '(lambda (x) (distance x p)) ipp))
    (foreach x xll
      (entdel x)
    )
    ;(foreach r (ListDupes dstl)
    ;  (entmake (list '(0 . "CIRCLE") (cons 10 (list (car p) (cadr p) 0.0)) (cons 40 r)))
    ;)
    (if (ListDupes dstl) t nil)
  )

  (defun inscribed-circle (p pol / plst xl xll ip ipp dstl)
    (setq
      plst (mapcar '(lambda (x) (list (car x) (cadr x) 0.0))
                   (mapcar 'cdr (acet-list-m-assoc 10 (entget pol)))
           )
    )
    (mapcar '(lambda (a b)
               (progn (setq xl
                             (entmakex (list '(0 . "XLINE") '(100 . "AcDbEntity") '(100 . "AcDbXline") (cons 10 a) (cons 11 (unit (mapcar '- b a)))))
                      )
                      (setq xll (cons xl xll))
               )
             )
            plst
            (cdr (reverse (cons (car plst) (reverse plst))))
    )
    (foreach x xll
      (setq ip (vlax-curve-getclosestpointto x p))
      (setq ipp (cons ip ipp))
    )
    (setq dstl (mapcar '(lambda (x) (distance x p)) ipp))
    (foreach x xll
      (entdel x)
    )
    (eval (cons 'min (ListDupes dstl)))
  ) 

  (defun insidep (pt entn / big flag obj1 obj2 obj3 p1 p2 small)
    (if (and pt entn)
      (progn
        (setq obj1 (vlax-ename->vla-object entn))
        (setq obj2 (car (vlax-invoke obj1 'Offset 0.001))
              obj3 (car (vlax-invoke obj1 'Offset -0.001))
        )
        (if (> (vla-get-area obj2) (vla-get-area obj3))
          (progn
            (set 'big obj2)
            (set 'small obj3)
          )
          (progn
            (set 'big obj3)
            (set 'small obj2)
          )
        )
        (setq p1 (vlax-curve-getClosestPointTo big pt)
              p2 (vlax-curve-getClosestPointTo small pt)
        )
        (if (> (distance pt p1) (distance pt p2))
          (setq flag T)
          (setq flag nil)
        )
        (mapcar (function (lambda (x)
                            (progn
                              (vla-delete x)
                              (vlax-release-object x)
                            )
                          )
                )
                (list big small)
        )
      )
    )
    flag
  )

  (defun unique (lst)
    (if lst
      (cons (car lst)
            (unique (_vl-remove (car lst) (cdr lst) 1e-6))
      )
    )
  )

  (defun _vl-remove (el lst fuzz)
    (vl-remove-if
      '(lambda (x)
         (and (equal (car x) (car el) fuzz)
              (equal (cadr x) (cadr el) fuzz)
         )
       )
      lst
    )
  )

  (defun ridge (v1 v2)
    (if (not
          (minusp (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1))))
        )
      (mapcar '*
              (list -1.0 -1.0 -1.0)
              (mapcar '- v1 v2)
      )
      (if (or (equal (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1)))
                     0.0
                     1e-8
              )
              (equal (- (* (car v1) (cadr v2)) (* (car v2) (cadr v1)))
                     -0.0
                     -1e-8
              )
          )
        (if (equal v1 v2 1e-8)
          (polar '(0.0 0.0 0.0)
                 (+ (angle '(0.0 0.0 0.0) v1) (/ pi 2.0))
                 1.0
          )
          v2
        )
        (mapcar '- v1 v2)
      )
    )
  )

  (defun onlin-p (p1 p2 p)
    (and
      (equal (distance p1 p2)
             (+ (distance p1 p) (distance p2 p))
             1e-8
      )
      (not (equal p1 p 1e-8))
      (not (equal p2 p 1e-8))
    )
  )

  (defun assocon (SearchTerm Lst func fuzz)
    (car
      (vl-member-if
        (function
          (lambda (pair)
            (equal SearchTerm (apply func (list pair)) fuzz)
          )
        )
        lst
      )
    )
  )

  (defun prelst (lst el / f)
    (vl-remove-if
      '(lambda (a) (or f (setq f (equal a el 1e-8))))
      lst
    )
  )

  (defun suflst (lst el)
    (cdr (vl-member-if '(lambda (a) (equal a el 1e-8)) lst))
  )

  (defun unit (v)
    (mapcar '(lambda (x) (/ x (distance '(0.0 0.0 0.0) v))) v)
  )

  (setq
    vl (mapcar
         'cdr
         (vl-remove-if-not
           '(lambda (x) (= (car x) 10))
           (entget pol)
         )
       )
  )
  (setq vl (cons (last vl) vl))
  (setq tl (mapcar '(lambda (a b) (mapcar '- b a)) vl (cdr vl)))
  (setq tl (mapcar '(lambda (x) (unit x)) tl))
  (setq vtl (mapcar '(lambda (a b) (list a b)) vl tl))
  (setq rl (mapcar '(lambda (a b) (ridge a b))
                   tl
                   (cdr (reverse (cons (car tl) (reverse tl))))
           )
  )
  (setq rl (mapcar '(lambda (x) (unit x)) rl))
  (setq vrl (mapcar '(lambda (a b) (list a b))
                    (cdr (reverse (cons (car vl) (reverse vl))))
                    rl
            )
  )
  (setq vrl (cons (last vrl) (reverse (cdr (reverse vrl)))))
  (setq pln T)

  (defun kr (lst / pl vpx1 vpxp1 vpx2 vpxp2 pp vxx z)
    (mapcar
      '(lambda (a b)
         (setq p
                (inters (car a)
                        (mapcar '+ (car a) (cadr a))
                        (car b)
                        (mapcar '+ (car b) (cadr b))
                        nil
                )
         )
         (setq pl (cons p pl))
       )
      (reverse (cons (car lst) (reverse lst)))
      (cdr (reverse (cons (car lst) (reverse lst))))
    )
    (setq vpl (mapcar '(lambda (a b c) (list (list a b) (list c b)))
                      vl
                      (reverse pl)
                      (cdr (reverse (cons (car vl) (reverse vl))))
              )
    )
    (setq vpl (apply 'append vpl))
;    (while (assocon nil vpl 'cadr 1e-6)
;      (setq vpl (vl-remove (assocon nil vpl 'cadr 1e-6) vpl))
;    )
    (setq pln nil)
    (foreach p pl
      (if (vl-some
            '(lambda (x)
               (vl-catch-all-apply 'onlin-p (list (car x) (cadr x) p))
             )
            vpl
          )
        (setq pln (cons p pln))
      )
    )
    (setq pln (mapcar '(lambda (x) (if (insidep x pol) x)) pln))
    (setq pln (vl-remove nil pln))
    (setq pln
      (mapcar
       '(lambda (x)
          (if (valid-inscribed-circle-p x pol) x)
        )
       pln
      )
    )
    (setq pln (vl-remove nil pln))
    (setq vx nil)
    (foreach p pln
      (mapcar '(lambda (x)
                 (if (equal (cadr x) p 1e-6)
                   (setq vx (cons x vx))
                 )
               )
              vpl
      )
    )
    (if (and (not (= (length pln) 1)) vx)
      (progn
        (setq vx (vl-sort vx
                          '(lambda (a b)
                             (< (inscribed-circle (cadr a) pol)
                                (inscribed-circle (cadr b) pol)
                             )
                           )
                 )
        )
        (if (and plnz (equal (cadr plnz) (inscribed-circle (cadar vx) pol) 1e-6))
          (if (equal (cadar vx) (cadadr vx) 1e-8)
            (if (> (distance (car plnz) (cadar vx)) (distance (car plnz) (cadr (caddr vx))))
              (setq vx (cons (caddr vx) vx))
            )
            (if (> (distance (car plnz) (cadar vx)) (distance (car plnz) (cadadr vx)))
              (setq vx (cons (cadr vx) vx))
            )
          )
        )
        (setq pln (list (cadar vx)))
      )
    )
    (if (null vx)
      (setq pln nil)
    )
    (if (vl-every '(lambda (x) (equal (cadar vpl) (cadr x) 1e-6))
                  vpl
        )
      (if (and (vl-every '(lambda (x) (eq (cadr x) nil)) vpl)
               (eq (length (unique vpl)) 2)
          )
        (progn
          (setq z  (entmakex (list '(0 . "LINE")
                                   (cons 10 (caar (unique vpl)))
                                   (cons 11 (caadr (unique vpl)))
                             )
                   )
                zz (cons z zz)
          )
          (setq pln nil)
        )
        (progn
          (foreach l (unique vpl)
            (setq z  (entmakex (list '(0 . "LINE")
                                     (cons 10 (car l))
                                     (cons 11 (cadr l))
                               )
                     )
                  zz (cons z zz)
            )
          )
          (setq pln nil)
        )
      )
    )
    (if (equal pln (list nil))
      (setq pln nil)
    )
    (if pln (setq plnz (list (car pln) (inscribed-circle (car pln) pol))))
    (foreach p pln
      (setq vp1 (cons (assocon p vpl 'cadr 1e-6) vp1))
      (setq vp2 (cons (assocon p (cdr (member (car vp1) vpl)) 'cadr 1e-6) vp2))
      (if (car vp1)
        (setq z  (entmakex (list '(0 . "LINE")
                                 (cons 10 (caar vp1))
                                 (cons 11 (cadar vp1))
                           )
                 )
              zz (cons z zz)
        )
      )
      (if (car vp2)
        (setq z  (entmakex (list '(0 . "LINE")
                                 (cons 10 (caar vp2))
                                 (cons 11 (cadar vp2))
                           )
                 )
              zz (cons z zz)
        )
      )
      (setq vpp2 (caar vp2))
      (setq v2 nil)
      (while (setq vpp2 (car (assocon vpp2 vp2 'cadr 1e-6)))
        (if (not (null vpp2))
          (setq v2 vpp2)
        )
      )
      (if (null v2)
        (setq v2 (caar vp2))
      )
      (setq vpp1 (caar vp1))
      (setq v1 nil)
      (while (setq vpp1 (car (assocon vpp1 vp1 'cadr 1e-6)))
        (if (not (null vpp1))
          (setq v1 vpp1)
        )
      )
      (if (null v1)
        (setq v1 (caar vp1))
      )
      (setq pp
             (list
               p
               (unit
                 (ridge
                   (if
                     (cadr
                       (last (prelst vtl (assocon v1 vtl 'car 1e-6)))
                     )
                      (cadr
                        (last (prelst vtl (assocon v1 vtl 'car 1e-6)))
                      )
                      (cadr (last vtl))
                   )
                   (cadr (assocon v2 vtl 'car 1e-6))
                 )
               )
             )
      )
      (setq vrl (if vrl
                  (subst pp (assocon (caar vp1) vrl 'car 1e-6) vrl)
                  (subst pp (assocon (caar vp1) lst 'car 1e-6) lst)
                )
            vrl (if (assocon (caar vp2) vrl 'car 1e-6)
                  (subst pp (assocon (caar vp2) vrl 'car 1e-6) vrl)
                )
      )
      (setq vl (subst p (caar vp1) vl)
            vl (subst p (caar vp2) vl)
      )
    )
  )

  (while pln (kr vrl))
)

(defun c:2droof-MR (/ *error* adoc plnz)

  (vl-load-com)
  (setq adoc (vla-get-activedocument (vlax-get-acad-object)))

  (defun *error* (msg)
    (if plnz
      (setq plnz nil)
    )
    (if zz
      (setq zz nil)
    )
    (if poly
      (setq poly nil)
    )
    (vla-endundomark adoc)
    (if msg
      (prompt msg)
    )
    (princ)
  )

  (vla-startundomark adoc)

  (setq poly
         (car
           (entsel
             "\nPick closed LWPOLYLINE in WCS oriented CCW and with only straight segments-edges"
           )
         )
  )

  (2droof poly)

  (*error* nil)

  (princ)
)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on February 11, 2014, 09:48:43 AM
Which One is right ?

The one on the left
Thanks Tim ! Sorry for reviting it too later .
The one on the right has a "Dead Vally" (a flat valley). This is to be avoided but sometime can not.
When you can't get away from the situation you add a "Cricket"  8-)
Alan,thank you too !  Sorry for reviting it too later .

---------------------------------------------------------------------------------
I think for this problem it can be solved by three way :
1. Offset method , just like here suggest : "Straight Skeletons for Roofs"  http://www.theswamp.org/index.php?topic=721.0
2. Construction 3d inwardly inclined plane, and get it's cast sight .
3. Use of physical methods, the film filled with water will form all fall ridge lines ; Or Eq Strong trace of the edge gravitational field .
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 11, 2014, 11:21:10 AM
Which One is right ?

The one on the left

Here is an interesting example... According to my routine-last posted one and GP's it seems that both soultion is correct, but in reality I would use right one, because of death valley and I would 2 parallel and close ridges implement as one...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: CAB on February 11, 2014, 11:29:08 AM
Garry's is closest but not quite.
This is correct.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: snownut2 on February 11, 2014, 11:32:14 AM
CAB,

Is correct, not sure if its the resolution but there appears to be a little jog in the ridge.  Also much easier to construct, than the death valley version.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: LE3 on February 11, 2014, 11:48:19 AM
Was curios, and end up rebuilding my old skeletonsRoof arx class for a2014, and I get this (see image):
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: GP on February 11, 2014, 11:54:34 AM
Which is correct?

The valleys must be sloped and start from the lower roof edge.


CAB,

Is correct, not sure if its the resolution but there appears to be a little jog in the ridge...

Yes, see posted dwg.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 15, 2014, 02:42:43 PM
Now there are no dead valleys... Both solutions are correct... If you don't believe, use my 2droof3d.lsp and check it in 3D... One on the left was made by my last 2droof-MR.lsp and one on the right by GP's HR.lsp... So now we stand for solution - which one do you like it more?

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 17, 2014, 04:54:41 AM
Another update of 2droof3d.lsp... You can find it here :

http://www.theswamp.org/index.php?topic=41837.msg513241#msg513241

Sorry for inconvenience... There was mistake in my subfunction ListClockwise-p which I fixed - the code was actually the same as old one - only this fix and now this is new version...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on February 22, 2014, 12:50:17 AM
Now there are no dead valleys... Both solutions are correct... If you don't believe, use my 2droof3d.lsp and check it in 3D... One on the left was made by my last 2droof-MR.lsp and one on the right by GP's HR.lsp... So now we stand for solution - which one do you like it more?

Regards, M.R.
I think the right is right . In MidLine ABC, |AB|<|AC|;it can be deduced by Offset-method , Outline offset to Point C will be continued, but offset to Point B will be break .
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on February 22, 2014, 01:08:14 AM
Another update of 2droof3d.lsp... You can find it here :

http://www.theswamp.org/index.php?topic=41837.msg513241#msg513241

Sorry for inconvenience... There was mistake in my subfunction ListClockwise-p which I fixed - the code was actually the same as old one - only this fix and now this is new version...

M.R.
I test your "2droof3d new old lisp" , it can't run well in my ACAD2011 , See the test Gif .
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 22, 2014, 10:12:34 AM
chlh_jd... But to use 2droof3d.lsp, you have to have 2d soultion firstly created... You just selected LWPOLYLINE without 2D solution ridge lines... And I suggest that you use simple 2droof3d.lsp - not the old version... As for the above example, I agree that this HP.lsp solution can be derived from built-in EXTRUDE - TAPER command, but then you'll miss possible other soultions like I showed on left example... Both are correct and more over - to my opinion, left solution is much nicer as in practical possible realization reasons there are no close ridge edges that collapse into top apex... There is much nicer top ridge that is then continued to transform into porche roof mass that is attached to main roof solution...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on February 22, 2014, 10:13:56 PM
chlh_jd... But to use 2droof3d.lsp, you have to have 2d soultion firstly created... You just selected LWPOLYLINE without 2D solution ridge lines... And I suggest that you use simple 2droof3d.lsp - not the old version... As for the above example, I agree that this HP.lsp solution can be derived from built-in EXTRUDE - TAPER command, but then you'll miss possible other soultions like I showed on left example... Both are correct and more over - to my opinion, left solution is much nicer as in practical possible realization reasons there are no close ridge edges that collapse into top apex... There is much nicer top ridge that is then continued to transform into porche roof mass that is attached to main roof solution...
I'll test 2droof3d.lsp later, I don't think the Left result in your above example is right , just I reply following it . One boundary-line only One Roof-line , I think .
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on March 15, 2014, 05:48:41 PM
2droof3d.lsp has been updated... Removed biggest boundary polyline from possible interaction within last lambda statement... Also previously created regions and lines successfully converted to LWPOLYLINES and removed from active document... (Implemented code from L.M. bbpoly.lsp posted here on this site)

http://www.theswamp.org/index.php?topic=10371.msg514603#msg514603

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on May 25, 2014, 04:57:37 AM
Also changed hr.lsp - GP's modified version by me... Little change inside Maximum circumscribed circle subfunction...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on May 31, 2014, 05:55:25 AM
Use Command method dose not run in my computer (Acad2011 & Win7 64Bits)
here's my first version use Ray and Axis to construct roof line .
Code - Auto/Visual Lisp: [Select]
  1. (setq _2pi (+ pi pi)
  2.       _pi2 (/ pi 2.)
  3.       *gsls_debug* nil)
  4.  
  5. (defun ray-inters  (p0 an0 p1 an1 / p)
  6.   ;; intersection of two ray line
  7.   ;; an0 an1 -- [0 2pi)
  8.   (if (equal (angle p0 p1) an0 5e-5)
  9.     p1
  10.     (if
  11.       (and (setq p (inters p0 (polar p0 an0 1000.) p1 (polar p1 an1 1000.) nil))
  12.            (equal (angle p0 p) an0 5e-5)
  13.            (equal (angle p1 p) an1 5e-5))
  14.        p)))
  15.  
  16. (defun bisetor  (p0 p1 p2 / d a a0)
  17.   ;; Get inter bisetor of counterclockwise point set
  18.   ;; p0 p1 p2 -- 3p of ccw polygon
  19.   ;; return inter-bisetor in [0 2pi)
  20.   (setq d (distance p0 p1)
  21.         a (- (setq a0 (angle p1 p0)) (angle p1 p2))
  22.         a (- a0 (/ (angle '(0 0) (list (* d (cos a)) (* d (sin a)))) 2.)))
  23.   (cond ((< a 0) (+ a _2pi))
  24.         ((>= a _2pi) (- a _2pi))
  25.         (a)))
  26. (defun bisetor2  (p0 p1 p2 p3)
  27.   ;; Get inter bisetor between line (p1 p0) with line (p2 p3)
  28.   ;; p0 p1 p2 p4-- 4p of ccw polygon
  29.   ;; return inter-bisetor in [0 2pi)
  30.   (setq p0 (mapcar (function +) (mapcar (function -) p2 p1) p0))
  31.   (if
  32.     (<= (car (trans (mapcar (function -) p0 p3) 0 (mapcar (function -) p2 p3))) 0)
  33.      (bisetor p0 p2 p3)
  34.      (bisetor p3 p2 p0)))
  35. ;;-----------------------------------------------------
  36. (defun lbisetor  (l / i)
  37.   (setq i -1)
  38.   (mapcar (function (lambda (p0 p1 p2)
  39.                       (setq i (1+ i))
  40.                       (cons i (bisetor p0 p1 p2))))
  41.           (cons (last l) l)
  42.           l
  43.           (append (cdr l) (list (car l)))))
  44. ;;
  45. (defun format-i  (i n)
  46.   (cond ((< i 0) (+ n i))
  47.         ((>= i n) (- i n))
  48.         (i)))
  49.  
  50. ;;;Function : judge a point location with polygon
  51. ;;;Arg : pt -- a point
  52. ;;;      pts -- points of polygon
  53. ;;;      eps -- allowance
  54. ;;;return :
  55. ;;;     -1 -- out of polygon , 0 -- at , 1 -- in
  56. (defun pipl?  (pt pts eps / is at a)
  57.   ;; by &#29378;&#20992;  
  58.   ;; Edit by GSLS(SS) 2011.03.28
  59.   ;; Solved the problem : if a point at the given polygon , it perhap return T or NIL .  
  60.   (cond
  61.     ((vl-some (function (lambda (x) (equal x pt eps))) pts) 0)
  62.     ((and
  63.        (equal
  64.          (abs
  65.            (apply
  66.              (function +)
  67.              (mapcar
  68.                (function (lambda (x y / a)
  69.                            (setq a (rem (- (angle pt x) (angle pt y)) PI))
  70.                            (if (equal (+ (distance pt x) (distance pt y))
  71.                                       (distance x y)
  72.                                       Eps)
  73.                              (setq at T))
  74.                            a))
  75.                (cons (last pts) pts)
  76.                pts)))
  77.          pi
  78.          eps)
  79.        (not at))
  80.      1)
  81.     (at 0)
  82.     (-1)))
  83. ;; get closed polygon's area
  84. (defun ss-pts2area  (l)
  85.   (/ (apply (function +)
  86.             (mapcar (function (lambda (x y)
  87.                                 (- (* (car x) (cadr y)) (* (car y) (cadr x)))))
  88.                     (cons (last l) l)
  89.                     l)) 2.))
  90. ;; round
  91. (defun round  (a jd / b s)
  92.   (cond ((numberp a)
  93.          (setq b (expt 10.0 jd)
  94.                s (sign a))
  95.          (/ (fix (+ (* a b) (* 0.5 s))) b))
  96.         ((listp a)
  97.          (mapcar (function (lambda (a) (round a jd))) a))))
  98. ;;
  99. (defun sign  (x)
  100.   (cond ((minusp x) -1.)(1.0)))
  101. ;;
  102. (defun inzone?  (p p0 an0 p1 an1 / f a an2 an3 d)
  103.   (defun f  (a)
  104.     (cond ((< a 0) (+ a _2pi))
  105.           (a)))
  106.   (setq a   (angle p0 p1)
  107.         an0 (f (- an0 a))
  108.         an1 (f (- an1 a))
  109.         an2 (f (- (angle p0 p) a))
  110.         an3 (f (- (angle p1 p) a))
  111.         d   (car (trans (mapcar (function -) p p1) 0 (mapcar (function -) p0 p1))))
  112.   (and (<= an2 (+ an0 1e-3)) (>= an3 (- an1 1e-3)) (< d 0)))
  113.  ;|
  114. (defun c:test (/ p0 p1 p2 p3 an0 an1 p)
  115.   (setq p0 (getpoint "\nSelect CCW 4 Points -first :")
  116.         p1 (getpoint p0)
  117.         p2 (getpoint p1)
  118.         p3 (getpoint p2)
  119.         an0 (angle p1 p0)
  120.         an1 (angle p2 p3)
  121.         p0 p1 p1 p2)
  122.   (while (setq p (getpoint "\nSelect Determine point :"))
  123.     (if (inzone? p p0 an0 p1 an1)
  124.       (alert "In Zone.")
  125.       (alert "Out Zone."))))
  126. |;
  127.  
  128. (defun c:test  (/ list-inters _Pedal suit-i pnth add-item suit _closed foo foo1 ;_loacal functions
  129.                 e pl n i sl bl il rgl tl a b c a1 b1 c1 p p0 p1 p2 p3 closed_n
  130.                 closed_il rdl sti fi bi ip0 ip1 j0 j1 rml)
  131.   ;;  Find the ridge lines of sloped roof
  132.   ;;  V0.1  by GSLS(SS)  May 31 , 2014
  133.   ;;  
  134.   ;;------------------------------------local functions------------------------------;;
  135.   ;;
  136.   (defun list-inters  (a b)
  137.     (vl-remove-if-not (function (lambda (x)
  138.                                   (vl-position x a)))
  139.                       b))
  140.   ;;
  141.   (defun _Pedal  (p p1 p2)
  142.     (inters p (polar p (+ _pi2 (angle p1 p2)) 1e3) p1 p2 nil))
  143.   (defun suit-i  (i f)
  144.     (if (not (assoc 70 (nth i rgl)))
  145.       i
  146.       (suit-i (format-i (f i) n) f)))
  147.  
  148.   ;;
  149.   (defun pnth  (a)
  150.     (cond ((= (car a) 10) (nth (cdr a) pl))
  151.           ((nth (cdr a) tl))))
  152.   ;;
  153.   (defun add-item  (ti an chain / a b c d)
  154.     (setq a (car chain)
  155.           b (cadr chain)
  156.           c (cons 11 ti)
  157.           d (cons 12 an))
  158.     (cond ((member c chain)
  159.            (while (not (equal (car chain) c))
  160.              (setq chain (cdr chain)))
  161.            chain)
  162.           ((and an (= (car a) 12))
  163.            (cons (cons 12 an) (cons (cons 11 ti) (cdr chain))))
  164.           ((and (not an) (= (car a) 12))
  165.            (cons (cons 11 ti) (cdr chain)))
  166.           ((= (car a) 11)
  167.            (cond ((= (cdr a) ti)
  168.                   (cons (cons 12 an) chain))
  169.                  ((equal (nth ti tl) (nth (cdr a) tl) 1e-6)
  170.                   (cons (cons 12 an) chain))
  171.                  (chain)))
  172.           (chain)))
  173.   ;;
  174.   (defun suit  (p si / s p1 p2 pp d i ssl)
  175.     (setq s  (nth si sl)
  176.           p1 (nth (car s) pl)
  177.           p2 (nth (cadr s) pl)
  178.           pp (_pedal p p1 p2)
  179.           d  (distance p pp)
  180.           i  -1)
  181.     (setq ssl (vl-remove-if (function (lambda (a)
  182.                                         (setq i (1+ i))
  183.                                         (member i closed_il)))
  184.                             sl))
  185.     (setq sdl
  186.            (mapcar
  187.              (function (lambda (s / p1 p2 an1 an2 pp)
  188.                          (setq p1  (nth (car s) pl)
  189.                                p2  (nth (cadr s) pl)
  190.                                an1 (cdr (assoc (car s) bl))
  191.                                an2 (cdr (assoc (cadr s) bl))
  192.                                pp  (_pedal p p1 p2))
  193.                          (if (or (inzone? p p1 an1 p2 an2)
  194.                                  (equal (distance p1 p2)
  195.                                         (+ (distance p1 pp) (distance p2 pp))
  196.                                         1e-3))
  197.                            (distance p pp)
  198.                            (min (distance p p1) (distance p p2)))))
  199.              ssl))
  200.     (setq sdl (vl-sort sdl '<))
  201.     (not (vl-some (function (lambda (d1) (not (equal d d1 1e-3))))
  202.                   (vl-remove nil (list (car sdl) (cadr sdl) (caddr sdl)))))
  203.     )
  204.   ;; check closed
  205.   (defun _closed  ()
  206.     (if (< closed_n n)
  207.       (setq rgl
  208.              (mapcar
  209.                (function
  210.                  (lambda (r / a b c d e)
  211.                    (setq a (car r)
  212.                          b (cadr r)
  213.                          c (caddr r))
  214.                    (cond
  215.                      (c r)
  216.                      ((and (setq d (assoc 11 a)) (setq e (assoc 11 b)) (equal d e))
  217.                       (setq closed_n  (1+ closed_n)
  218.                             closed_il (cons (cdr (assoc 10 a)) closed_il))
  219.                       (list (member d a) (member e b) (cons 70 1)))
  220.                      ((list a b)))))
  221.                rgl))))
  222.   ;;------------------------------------main routine ------------------------------;;
  223.   (setq e (car (entsel "\nSelect a closed polygon :")))
  224.   (setq pl
  225.          (mapcar (function cdr)
  226.                  (vl-remove-if-not (function (lambda (a) (= (car a) 10))) (entget e))))
  227.   (if (< (ss-pts2area pl) 0) ;_force pointset CCW
  228.     (setq pl (reverse pl)))
  229.   ;; build side message
  230.   (setq n (length pl)
  231.         i -1)
  232.   (setq sl (mapcar (function (lambda (a)
  233.                                (setq i (1+ i))
  234.                                (if (= (1+ i) n)
  235.                                  (list i 0)
  236.                                  (list i (1+ i)))))
  237.                    pl))
  238.   ;; build inter-bisetor angle list with point index
  239.   (setq bl (lbisetor pl))
  240.   ;; for test
  241.   (if *gsls_debug* ;_(setq *gsls_debug* T)
  242.     (mapcar (function (lambda (a / p p1)
  243.                         (setq p  (nth (car a) pl)
  244.                               p1 (polar p (cdr a) 500.))
  245.                         (entmake (list (cons 0 "TEXT")
  246.                                        (cons 10 p)
  247.                                        (cons 1 (rtos (car a) 2 0))
  248.                                        (cons 40 1000.)
  249.                                        (cons 41 0.7)
  250.                                        (cons 50 0.0)))
  251.                         (entmake (list (cons 0 "LINE")
  252.                                        (cons 10 p)
  253.                                        (cons 11 p1)
  254.                                        (cons 8 "TEST")
  255.                                        (cons 62 3)))))
  256.             bl))
  257.   ;; build start result list , each side with two construction list
  258.   ;; dxf code : 10 -- point of pl , 11 -- point of tl (inter points) , 12 -- ray angle of last point  , 70 -- closed mark
  259.   (setq rgl
  260.          (mapcar
  261.            (function
  262.              (lambda (s)
  263.                (list (list (cons 12 (cdr (nth (car s) bl))) (cons 10 (car s)))
  264.                      (list (cons 12 (cdr (nth (cadr s) bl))) (cons 10 (cadr s))))))
  265.            sl))
  266.   ;; find first suitable ray-intersections
  267.   (setq il
  268.          (vl-remove
  269.            nil
  270.            (mapcar
  271.              (function
  272.                (lambda (s / s1 s2 p i)
  273.                  (setq s1 (nth (car s) bl) ;_(setq s (list 19 20))
  274.                        s2 (nth (cadr s) bl)
  275.                        p  (ray-inters (nth (car s1) pl)
  276.                                       (cdr s1)
  277.                                       (nth (car s2) pl)
  278.                                       (cdr s2)))
  279.                  (if
  280.                    (and
  281.                      p
  282.                      (> (pipl? p pl 1e-6) 0)
  283.                      (suit p (car s)))
  284.                     (list s p))))
  285.              sl)))
  286.   ;; add first suitable intersections into it's side info , put them closed .
  287.   (setq closed_n 0)
  288.   (foreach s  il
  289.     (setq p (round (cadr s) 6))
  290.     (if (not (setq i (vl-position p tl)))
  291.       (setq tl (append tl (list p))
  292.             i  (1- (length tl))))
  293.     (setq b         (nth (caar s) rgl)
  294.           b1        (list (add-item i nil (car b))
  295.                           (add-item i nil (cadr b))
  296.                           (cons 70 1))
  297.           rgl       (subst b1 b rgl)
  298.           closed_n  (1+ closed_n)
  299.           closed_il (cons (caar s) closed_il))
  300.     ;;deal near side info
  301.     (if (and (< closed_n n)
  302.              (setq bi (suit-i (format-i (1- (caar s)) n) 1-))
  303.              (setq fi (suit-i (cadar s) 1+))
  304.              (/= bi fi))
  305.       (setq a   (nth bi rgl)
  306.             c   (nth fi rgl)
  307.             p0  (nth (cdr (assoc 10 (car a))) pl)
  308.             p1  (nth (cdr (assoc 10 (cadr a))) pl)
  309.             p2  (nth (cdr (assoc 10 (car c))) pl)
  310.             p3  (nth (cdr (assoc 10 (cadr c))) pl)
  311.             an  (bisetor2 p0 p1 p2 p3)
  312.             a1  (list (car a) (add-item i an (cadr a)))
  313.             c1  (list (add-item i an (car c)) (cadr c))
  314.             rgl (subst a1 a rgl)
  315.             rgl (subst c1 c rgl))))
  316.   ;; deal has closed side .
  317.   (_closed)
  318.   ;; deal near side of closed-side , until catch another closed side
  319.   (if (< closed_n n)
  320.     (setq rdl (vl-remove-if-not (function (lambda (a) (assoc 70 a))) rgl)
  321.           rdl (mapcar (function (lambda (a / p pp d)
  322.                                   (if (assoc 11 (car a))
  323.                                     (setq p (pnth (assoc 11 (car a))))
  324.                                     (setq p (pnth (assoc 11 (cadr a)))))
  325.                                   (setq pp (_pedal p
  326.                                                    (pnth (assoc 10 (car a)))
  327.                                                    (pnth (assoc 10 (cadr a))))
  328.                                         d  (distance p pp))
  329.                                   (list d a)))
  330.                       rdl)
  331.           rdl (vl-sort rdl
  332.                        (function (lambda (a b)
  333.                                    (< (car a) (car b)))))))
  334.   (defun foo  ()
  335.     (while (and (< closed_n n)
  336.                 (/= bi fi)
  337.                 (setq a (nth bi rgl))
  338.                 (setq c (nth fi rgl))
  339.                 (not (assoc 70 a))
  340.                 (not (assoc 70 c))
  341.                 (vl-remove nil
  342.                            (list (setq ip0 (ray-inters (pnth (cadadr a))
  343.                                                        (cdaadr a)
  344.                                                        (pnth (cadar a))
  345.                                                        (cdaar a)))
  346.                                  (setq ip1 (ray-inters (pnth (cadar c))
  347.                                                        (cdaar c)
  348.                                                        (pnth (cadadr c))
  349.                                                        (cdaadr c)))))
  350.                 (setq p (pnth (cadar c)))
  351.                 (setq ip
  352.                        (cond
  353.                          ((and ip0 ip1)
  354.                           (cond
  355.                             ((and (> (setq j0 (pipl? ip0 pl 1e-6)) 0)
  356.                                   (> (setq j1 (pipl? ip1 pl 1e-6)) 0))
  357.                              (cond ((<= (distance p ip0) (distance p ip1))
  358.                                     (if (suit ip0 bi)
  359.                                       (cons "back" ip0)))
  360.                                    ((if (suit ip1 fi)
  361.                                       (cons "for" ip1)))))
  362.                             ((> (setq j0 (pipl? ip0 pl 1e-6)) 0)
  363.                              (if (suit ip0 bi)
  364.                                (cons "back" ip0)))
  365.                             ((> (setq j1 (pipl? ip1 pl 1e-6)) 0)
  366.                              (if (suit ip1 fi)
  367.                                (cons "for" ip1)))))
  368.                          ((and ip0 (> (setq j0 (pipl? ip0 pl 1e-6)) 0))
  369.                           (if (suit ip0 bi)
  370.                             (cons "back" ip0)))
  371.                          ((and ip1 (> (setq j1 (pipl? ip1 pl 1e-6)) 0))
  372.                           (if (suit ip1 fi)
  373.                             (cons "for" ip1))))))
  374.       (setq p (round (cdr ip) 6))
  375.       (if (not (setq i (vl-position p tl)))
  376.         (setq tl (append tl (list p))
  377.               i  (1- (length tl))))
  378.       (cond ((= (car ip) "for")
  379.              (setq c1        (list (add-item i nil (car c))
  380.                                    (add-item i nil (cadr c))
  381.                                    (cons 70 1))
  382.                    rgl       (subst c1 c rgl)
  383.                    closed_n  (1+ closed_n)
  384.                    closed_il (cons (cdr (assoc 10 (car c))) closed_il)
  385.                    fi        (format-i (1+ fi) n))
  386.              (if (= (suit-i (format-i fi n) 1+) bi)
  387.                (setq a1        (list (add-item i nil (car a))
  388.                                      (add-item i nil (cadr a))
  389.                                      (cons 70 1))
  390.                      rgl       (subst a1 a rgl)
  391.                      closed_n  (1+ closed_n)
  392.                      closed_il (cons (cdr (assoc 10 (car a))) closed_il))
  393.                (if (setq c (nth (suit-i (format-i fi n) 1+) rgl))
  394.                  (setq p2  (pnth (assoc 10 (car c)))
  395.                        p3  (pnth (assoc 10 (cadr c)))
  396.                        p0  (pnth (assoc 10 (car a)))
  397.                        p1  (pnth (assoc 10 (cadr a)))
  398.                        an  (bisetor2 p0 p1 p2 p3)
  399.                        a1  (list (car a) (add-item i an (cadr a)))
  400.                        rgl (subst a1 a rgl)
  401.                        c1  (list (add-item i an (car c)) (cadr c))
  402.                        rgl (subst c1 c rgl)))))
  403.             ((= (car ip) "back")
  404.              (setq a1        (list (add-item i nil (car a))
  405.                                    (add-item i nil (cadr a))
  406.                                    (cons 70 1))
  407.                    rgl       (subst a1 a rgl)
  408.                    closed_n  (1+ closed_n)
  409.                    closed_il (cons (cdr (assoc 10 (car a))) closed_il)
  410.                    bi        (format-i (1- bi) n))
  411.              (if (= (suit-i (format-i bi n) 1-) fi)
  412.                (setq c1        (list (add-item i nil (car c))
  413.                                      (add-item i nil (cadr c))
  414.                                      (cons 70 1))
  415.                      rgl       (subst c1 c rgl)
  416.                      closed_n  (1+ closed_n)
  417.                      closed_il (cons (cdr (assoc 10 (car c))) closed_il))
  418.                (if (setq a (nth (suit-i (format-i bi n) 1-) rgl))
  419.                  (setq p2  (pnth (assoc 10 (car c)))
  420.                        p3  (pnth (assoc 10 (cadr c)))
  421.                        p0  (pnth (assoc 10 (car a)))
  422.                        p1  (pnth (assoc 10 (cadr a)))
  423.                        an  (bisetor2 p0 p1 p2 p3)
  424.                        a1  (list (car a) (add-item i an (cadr a)))
  425.                        rgl (subst a1 a rgl)
  426.                        c1  (list (add-item i an (car c)) (cadr c))
  427.                        rgl (subst c1 c rgl)))))) ;_cond      
  428.       ))
  429.   ;;
  430.   (foreach s  rdl
  431.     (if (< closed_n n)
  432.       (progn
  433.         (setq sti (cdr (assoc 10 (caadr s))))
  434.         (setq fi (suit-i (format-i (1+ sti) n) 1+)
  435.               bi (suit-i (format-i (1- sti) n) 1-))
  436.         (foo)))) ;_foreach
  437.   ;; check closed  
  438.   (_closed)
  439.   ;; deal other no closed side
  440.   (defun foo1  (a)
  441.     (if (and (setq p (ray-inters (pnth (cadadr a))
  442.                                  (cdaadr a)
  443.                                  (pnth (cadar a))
  444.                                  (cdaar a)))
  445.              (> (pipl? p pl 1e-6) 0)
  446.              (suit p (cdr (assoc 10 (car a)))))
  447.       (progn
  448.         (setq p (round (cdr ip) 6))
  449.         (if (not (setq i (vl-position p tl)))
  450.           (setq tl (append tl (list p))
  451.                 i  (1- (length tl))))
  452.         (setq a1        (list (add-item i nil (car a))
  453.                               (add-item i nil (cadr a))
  454.                               (cons 70 1))
  455.               rgl       (subst a1 a rgl)
  456.               closed_n  (1+ closed_n)
  457.               closed_il (cons (cdr (assoc 10 (car a))) closed_il)
  458.               ))
  459.       (if (equal (angle (pnth (cadar a)) (pnth (cadadr a)))
  460.                  (cdaar a)
  461.                  1e-5) ;_collinear
  462.         (setq a1        (list (cons (cadadr a) (cdar a))
  463.                               (cdadr a)
  464.                               (cons 70 1))
  465.               rgl       (subst a1 a rgl)
  466.               closed_n  (1+ closed_n)
  467.               closed_il (cons (cdr (assoc 10 (car a))) closed_il))
  468.         (if (equal (cadadr a) (cadar a))
  469.           (setq a1        (list (cdar a) (cdadr a) (cons 70 1))
  470.                 rgl       (subst a1 a rgl)
  471.                 closed_n  (1+ closed_n)
  472.                 closed_il (cons (cdr (assoc 10 (car a))) closed_il))
  473.           (progn ;_force out routine ...
  474.             (princ "\n ***Error*** : ...")
  475.             (setq closed_n (1+ closed_n)))
  476.           ))))
  477.   (while (and (< closed_n n)
  478.               (setq rml (vl-remove-if (function (lambda (a) (assoc 70 a))) rgl)))
  479.     (cond
  480.       ((caddr rml)
  481.        (setq il
  482.               (vl-remove
  483.                 nil
  484.                 (mapcar
  485.                   (function
  486.                     (lambda (a / i p b)
  487.                       (setq i (vl-position a rgl))
  488.                       (if (and (setq p (ray-inters (pnth (cadadr a))
  489.                                                    (cdaadr a)
  490.                                                    (pnth (cadar a))
  491.                                                    (cdaar a)))
  492.                                (> (pipl? p pl 1e-6) 0)
  493.                                (suit p i)
  494.                                (setq b (list (cadar a) (cadadr a))))
  495.                         (list p i))))
  496.                   rml))) ;_
  497.        ;;
  498.        (if il
  499.          (foreach s  il
  500.            (if (< closed_n n)
  501.              (progn
  502.                (setq p (car s)
  503.                      b (nth (cadr s) rgl))
  504.                (if (not (setq i (vl-position p tl)))
  505.                  (setq tl (append tl (list p))
  506.                        i  (1- (length tl))))
  507.                (setq b1        (list (add-item i nil (car b))
  508.                                      (add-item i nil (cadr b))
  509.                                      (cons 70 1))
  510.                      rgl       (subst b1 b rgl)
  511.                      closed_n  (1+ closed_n)
  512.                      closed_il (cons (cdr (assoc 10 (car b))) closed_il))
  513.                (setq fi (suit-i (format-i (1+ (cdr (assoc 10 (car b)))) n) 1+)
  514.                      bi (suit-i (format-i (1- (cdr (assoc 10 (car b)))) n) 1-))
  515.                (setq a   (nth bi rgl)
  516.                      c   (nth fi rgl)
  517.                      p0  (nth (cdr (assoc 10 (car a))) pl)
  518.                      p1  (nth (cdr (assoc 10 (cadr a))) pl)
  519.                      p2  (nth (cdr (assoc 10 (car c))) pl)
  520.                      p3  (nth (cdr (assoc 10 (cadr c))) pl)
  521.                      an  (bisetor2 p0 p1 p2 p3)
  522.                      a1  (list (car a) (add-item i an (cadr a)))
  523.                      c1  (list (add-item i an (car c)) (cadr c))
  524.                      rgl (subst a1 a rgl)
  525.                      rgl (subst c1 c rgl))
  526.                (foo)))
  527.            )
  528.          (mapcar (function foo1) rml) ;_force deal
  529.          )
  530.        )
  531.       ((mapcar (function foo1) rml))) ;_cond
  532.     (_closed)
  533.     ) ;_while
  534.   ;; test result
  535.   (foreach r  rgl
  536.     (if (assoc 70 r)
  537.       (mapcar
  538.         (function (lambda (a)
  539.                     (setq b (mapcar (function (lambda (a) (pnth a))) a))
  540.                     (mapcar (function (lambda (c d)
  541.                                         (entmake (list (cons 0 "LINE")
  542.                                                        (cons 10 c)
  543.                                                        (cons 11 d)
  544.                                                        (cons 8 "Proof")
  545.                                                        (cons 62 1)))))
  546.                             b
  547.                             (cdr b))))
  548.         (list (car r) (cadr r)))))
  549.   (princ)
  550.   )
  551.  
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on May 31, 2014, 06:38:49 AM
Very impressive, GSLS(SS)... Still not all cases are solvable... Look into my example DWG...

But thanks for reply, nice coding...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on May 31, 2014, 06:59:03 AM
Another example...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 02, 2014, 03:31:13 AM
Ribar , thank you very much . :-)
I'v changed the routine's loop and some function to do better , but even found a no suit shape pl .
To run routine's , you must download the lisp file .
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test  (/ list-inters _Pedal suit-i used-si pnth add-item suit _closed
  2.                 _getclosed _getneighbor _getclosedinfo _closeditself _get_minz
  3.                 _getnoclosedpair _getnoclosdedpairinfo foo foo1 foo2 ;_loacal functions
  4.                 pl n i% sl bl rgl tl zl closed_n closed_il ;_through variables
  5.                 e i il rdl rnl rml a b c a1 b1 c1 p p0 p1 p2 p3 z sti bi fi ip0 ip1
  6.                 j0 j1 sil)
  7.   ;;  Find the ridge lines of sloped roof
  8.   ;;  V0.2  by GSLS(SS)  June 31 , 2014
  9.   ;;  
  10.   ;;------------------------------------local functions------------------------------;;
  11.   ;;
  12. ....)
  13.  
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 02, 2014, 06:04:01 AM
Hi, chlh_jd... I must say I am very impressed... It almost work in every situation... I don't know why, by my "Another example" didn't finish like you showed on your animated GIF... I am on A2014 and didn't modify file I posted, just checked on the same polyline and it failed around start/end vertex... I've turned off debug mode, removed layer creation of ridge lines - uses current layer and done overkilling duplicate ridges through ALISP... And you showed on very complex example with revcloud some lack... Hope you'll solve this when you did it so good so far... I'll attach your lisp with those mods I mentioned (I don't want duplicate ridges)... Very grateful for this, thanks again GSLS(SS)...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: GDF on June 02, 2014, 10:59:49 AM
pretty impressive
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 02, 2014, 02:37:35 PM
chlh_jd, I've tried to improve, but I don't know if my attempt is good... Now my case is solved, but I don't know ab your (revcloud)... My attempt was pretty simple in comparison to your coding (made main sub-function - "start", and if it runs on error it restarts main function with little modified order of vertices - had to nil rgl variable)... So error should be printed, but solution should be made if my estimations were correct... Also made better formatting of code - never mind it's now bigger...

So long from me for now, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 02, 2014, 08:59:42 PM
Hi, chlh_jd... I must say I am very impressed... It almost work in every situation... I don't know why, by my "Another example" didn't finish like you showed on your animated GIF... I am on A2014 and didn't modify file I posted, just checked on the same polyline and it failed around start/end vertex... I've turned off debug mode, removed layer creation of ridge lines - uses current layer and done overkilling duplicate ridges through ALISP... And you showed on very complex example with revcloud some lack... Hope you'll solve this when you did it so good so far... I'll attach your lisp with those mods I mentioned (I don't want duplicate ridges)... Very grateful for this, thanks again GSLS(SS)...

M.R.
You are welcome .

chlh_jd, I've tried to improve, but I don't know if my attempt is good... Now my case is solved, but I don't know ab your (revcloud)... My attempt was pretty simple in comparison to your coding (made main sub-function - "start", and if it runs on error it restarts main function with little modified order of vertices - had to nil rgl variable)... So error should be printed, but solution should be made if my estimations were correct... Also made better formatting of code - never mind it's now bigger...

So long from me for now, M.R.
Ribar , Thank you for test , please post your error polygon for me , thanks .

I've catch the error you said , it's because of the accuracy of AutoCAD's VLIDE .
Even now the most Process of the Algorithm -- through bisector and axis from 2D , you can check every function with precision , and then found which is wrong .
Add change start point , shoud change each side to operate , and it can slove some error cause by precision .  In my opinion , change every point's coordinates into according the centroid , and then transform them back after get the result , this method will be some better .

It  can  be easy modified into support 3D method , because it has Z list , like building each side  region and then union them . Enjoy .
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 03, 2014, 12:31:13 AM
Ribar , Thank you for test , please post your error polygon for me , thanks .

Here is it - see attachment...

[EDIT] : Also if you plan to use my modified version of your code, change this :
Code: [Select]
          (princ "\n ***Error*** : ...")
          (setq rgl nil)
          (setq err (1+ err))
          (setq pl (cons (last pl) (reverse (cdr (reverse pl)))))
          (if (eq err 1) (start pl))
To this, so it can exchange all points and attempt to solve for each combination :
Code: [Select]
          (princ "\n ***Error*** : ...")
          (setq rgl nil)
          (setq err (1+ err))
          (setq pl (cons (last pl) (reverse (cdr (reverse pl)))))
          (if (< err n) (start pl))

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 03, 2014, 05:28:01 AM
Ribar , this one has fix the bug in the case of the dwg you post
The first closed side vectexs , must determin in the other  inzoneside triangle .
Code: [Select]
    ;; if the point in the triangle {pa pb [i0~3]} which construct by the inzone side {pa pb}
    ;;                        and it's bisector rays inters with line {p p1} or line {p p2} 
    ;; SCH. of Invalid ray-inters of side {p1 p2}         
    ;;         p2___________p1                     
    ;;           \i1       /                       
    ;;         /\      /             
    ;;          /   \\   /               
    ;;       bi_a  /      \ \/               
    ;;        |  /         \ /\bi_b           
    ;;         |/            P   \             
    ;;         pa-------------------pb                     
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 03, 2014, 07:18:10 AM
Congratulations, GSLS... What is the problem then with revcloud? I also have issues with arced polylines... But solving my posted DWG, I hope situation is much better... chlh_jd, do I have to use my attached version to solve posted DWG "Another example" or do you also have some better approach? I'll attach my version of implemented your new solution - this is what I use finally for now... Thanks again... M.R.

[EDIT] : Attached one more case...
[EDIT] : LSP file reattached so that now if solution was found no need for overkill of ridges...
(4 downloads of old version)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 03, 2014, 08:05:11 AM
One more situation...

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 03, 2014, 10:24:21 PM
Congratulations, GSLS... What is the problem then with revcloud? I also have issues with arced polylines... But solving my posted DWG, I hope situation is much better... chlh_jd, do I have to use my attached version to solve posted DWG "Another example" or do you also have some better approach? I'll attach my version of implemented your new solution - this is what I use finally for now... Thanks again... M.R.
[EDIT] : Attached one more case...
[EDIT] : LSP file reattached so that now if solution was found no need for overkill of ridges...
(4 downloads of old version)

Ribar , thank you for test . New version has fix "revcloud" bug and the case you post .

Don't devide arc so little , because  when the angle close to zero or close to pi , little angle deviation will cause large wrong point distance (like in the picture). To slove it , you must force angle pricision higher , such as 1e-9~1e-14, at the same time you should open vectexs equal eps , I don't sure (equal p1 p2 1e-3) is fit . To get  higher pricision , Recursive loop may be slow , perhaps  vlide is not suitable to do it .
This is why I used Lee Mac's function 'LM:Entity->Pointlist' , Of course, you cloud test all you used case and then set suitable angle and point pricision . Enjoy .


Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 04, 2014, 07:04:30 AM
Preliminary add command mode .
Solid make failure , Need more improved work .
Enjoy .
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 04, 2014, 12:35:07 PM
Preliminary add command mode .
Solid make failure , Need more improved work .
Enjoy .

chlh_jd, I use for modelling 2droof3d.lsp posted here on my previous posts... It works everything on my comp... Only thing you should say is that polyline should be scaled approx. 1000 times to avoid errors... I'll attach your code with my add of making ridges as single lines... This is prepared for 2droof3d.lsp... Thank you very much GSLS... I lost time checking what was the issue with errors and then I simply scaled and it worked... Congratulations on this code - it's very powerful...

Marko

[EDIT] : Reattached LSP to inform and prompt for scale factors if polyline wasn't scaled correctly...
( 11 downloads )
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: CAB on June 04, 2014, 01:03:59 PM
chlh_jd
Awesome work  :)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 04, 2014, 09:24:53 PM

chlh_jd, I use for modelling 2droof3d.lsp posted here on my previous posts... It works everything on my comp... Only thing you should say is that polyline should be scaled approx. 1000 times to avoid errors... I'll attach your code with my add of making ridges as single lines... This is prepared for 2droof3d.lsp... Thank you very much GSLS... I lost time checking what was the issue with errors and then I simply scaled and it worked... Congratulations on this code - it's very powerful...

Marko
Ribar , thank you for test every time , you're very welcome  :-)
I'm very impatient , so I don't let the sides to be take out 1000 times .

chlh_jd
Awesome work  :)
Thanks Alan  :-)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 05, 2014, 04:37:14 AM
chlh_jd,
Still more problems, see attached DWG (if you have time for coding - I am pleased with solution like it is, but...)...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 05, 2014, 11:02:26 AM
chlh_jd,
Still more problems, see attached DWG (if you have time for coding - I am pleased with solution like it is, but...)...
M.R.
Ribar , thank you for test .
I must clear that , the whole routine I wrote used midle speeded-up thread , so it won't work for every case , rigorous treatment is before each added vertex  or  each closed side must be reordered Z-axis height, started from Z smaller , one step by one step .

First & third case , it's the error cause by first speeded-up used while loop instead of if in  foo or foo1 function  , change it like following code ,will take it .
Second case , it's the error : has wrong vectex in first joined , must add determine ---no cross the first can be closed side's neighbor next bisector .
Code - Auto/Visual Lisp: [Select]
  1. (defun foo  ()
  2.     (_getclosed)
  3.     (_getneighbor)
  4.     (while (and (< closed_n n) rnl)
  5.       (foreach ip  rnl
  6.         (if (< closed_n n)
  7.           (progn (_closeditself ip)
  8.                 ;| (while       (and (< closed_n n) (setq ip (_getclosedinfo bi fi)))
  9.                    (_closeditself ip))|;
  10.             (if (and (< closed_n n) (setq ip (_getclosedinfo bi fi)))
  11.                    (_closeditself ip));_not quickly launch         
  12.             )))
  13.       (_getclosed)
  14.       (_getneighbor))
  15.     )
  16. (defun foo1  ()
  17.     (_getnoclosdedpairinfo)
  18.     (while (and (< closed_n n) rnl)
  19.       (foreach ip  rnl
  20.         (if (< closed_n n)
  21.           (progn (_closeditself ip)
  22.                  ;|(while       (and (< closed_n n) (setq ip (_getclosedinfo bi fi)))
  23.                    (_closeditself ip))|;
  24.                 (if     (and (< closed_n n) (setq ip (_getclosedinfo bi fi)))
  25.                    (_closeditself ip))     
  26.                    )
  27.           ))
  28.       (_getnoclosdedpairinfo)))
  29.  

New version : add drawmode , error handle ... etc.
The errors Ribar take it out , 1st & 3rd has been solve , the second even not .
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: hanhphuc on June 05, 2014, 12:19:14 PM
hi chlh_jd,
i just notice small bug, assume we open a new drawing,
so layer "Proof" does not exist
(setvar "CLAYER" "Proof") ;rejected?
my suggest (if (tblsearch "LAYER" "proof") then etc..

Just curiuos, my VLIDE inspects the REAL _pi2: 1.5707963267948966192313216916395, it returns 1.5708. if (* _pi2 100000.); returns 157080.0 does it mean the precision reduced?

Thanx your amazing code  :-)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 05, 2014, 01:25:42 PM
Here, I've modified my version of your code... So now there are 2 versions - while version (2droof.lsp) and if version (2droof-t.lsp)... Sometime like in my posted DWG, maybe it's better to use while version... 2droof-t.lsp is newest one... Note that both are the same just one uses while, and other if...

Thanks...

[EDIT] : LSP files reattached to include newest mods by chlh_jd posted in Proof_6a.lsp
(2droof-t.lsp - 2 downloads)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 05, 2014, 09:30:00 PM
hi chlh_jd,
i just notice small bug, assume we open a new drawing,
so layer "Proof" does not exist
(setvar "CLAYER" "Proof") ;rejected?
my suggest (if (tblsearch "LAYER" "proof") then etc..

Just curiuos, my VLIDE inspects the REAL _pi2: 1.5707963267948966192313216916395, it returns 1.5708. if (* _pi2 100000.); returns 157080.0 does it mean the precision reduced?

Thanx your amazing code  :-)
Thanks hanhphuc .
It did need check layer , in my post lisp file proof_6.lsp has add it.

for test it's precision use (rtos (* _pi2 1000000) 2 10) --> "1570796.326794896"

Here, I've modified my version of your code... So now there are 2 versions - while version (2droof.lsp) and if version (2droof-t.lsp)... Sometime like in my posted DWG, maybe it's better to use while version... 2droof-t.lsp is newest one... Note that both are the same just one uses while, and other if...

Thanks...
For get the second case in your post dwgs , need determine neighbor first closed before add the vertex into RGL .
See the proof-6a.lsp
Code - Auto/Visual Lisp: [Select]
  1. ...
  2. ;; add first suitable intersections into it's side info , put them closed .
  3.        (setq closed_n 0)
  4.        (foreach s  il
  5.          (setq p (round (cadr s) 5))
  6.          (setq a (nth (format-i (- (caar s) 2) n) rgl)
  7.                c (nth (format-i (+ (caar s) 2) n) rgl))
  8.          ;; add whether the neighbor closed first .
  9.          (cond ((and (assoc 70 (nth (format-i (- (caar s) 2) n) rgl))
  10.                      (setq a (nth (format-i (1- (caar s)) n) rgl))
  11.                      (setq p0 (ray-int-line (pnth (cadar a))
  12.                                             (cdaar a)
  13.                                             (pnth (assoc 10 (cadr a)))
  14.                                             p))
  15.                      (not (equal p0 p *gsls_disfuzz*))))
  16.                ((and (assoc 70 (nth (format-i (+ (caar s) 2) n) rgl))
  17.                      (setq c (nth (format-i (1+ (caar s)) n) rgl))
  18.                      (setq p1 (ray-int-line (pnth (cadadr c))
  19.                                             (cdaadr c)
  20.                                             (pnth (assoc 10 (car c)))
  21.                                             p))
  22.                      (not (equal p1 p *gsls_disfuzz*))))
  23.                (t
  24.                 (if (not (setq i (vl-position p tl)))
  25.                   (setq tl (append tl (list p))
  26.                         zl (append zl (list (caddr s)))
  27.                         i  (1- (length tl))))
  28.                 (setq b         (nth (caar s) rgl)
  29.                       b1        (list (add-item i nil (car b))
  30.                                       (add-item i nil (cadr b))
  31.                                       (cons 70 1))
  32.                       rgl       (subst b1 b rgl)
  33.                       closed_n  (1+ closed_n)
  34.                       closed_il (cons (caar s) closed_il))
  35.                 ;;deal near side info
  36.                 (if (and (< closed_n n)
  37.                          (setq bi (suit-i (format-i (1- (caar s)) n) 1-))
  38.                          (setq fi (suit-i (cadar s) 1+))
  39.                          (/= bi fi))
  40.                   (setq a   (nth bi rgl)
  41.                         c   (nth fi rgl)
  42.                         p0  (nth (cdr (assoc 10 (car a))) pl)
  43.                         p1  (nth (cdr (assoc 10 (cadr a))) pl)
  44.                         p2  (nth (cdr (assoc 10 (car c))) pl)
  45.                         p3  (nth (cdr (assoc 10 (cadr c))) pl)
  46.                         an  (bisector2 p0 p1 p2 p3)
  47.                         a1  (list (car a) (add-item i an (cadr a)))
  48.                         c1  (list (add-item i an (car c)) (cadr c))
  49.                         rgl (subst a1 a rgl)
  50.                         rgl (subst c1 c rgl))))))
  51.        ;; deal has closed side .
  52.        (_closed)
  53. ...
  54.  
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 06, 2014, 02:52:19 AM
chlh_jd, only if you have spare time... Look attached DWG... Hope I am not exaggerating...

(My posted LSP files updated and reattached in my previous post)

M.R.

[EDIT] : Attached one more...

Thanks...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 06, 2014, 05:52:19 AM
Just to mention, SOLUTION WASNT FINISHED.DWG is solvable through multiple attempts with error prompts from one of 2 versions - I think (while) 2droof.lsp...

Just uncomment these lines :
Code: [Select]
...
        (progn ;_force out routine ...
          (princ "\n ***Error*** : ...")
                                        (setq rgl nil)
                                        (setq errr (1+ errr))
                                        (setq pl (cons (last pl) (reverse (cdr (reverse pl)))))
                                        (if (and (< errr n) (not err))
                                        (start pl)
                                        )
...

And this - note (setq err t) :

Code: [Select]
...
    (while li
      (if
        (not
          (vl-some (function (lambda (x)
                               (or (and (equal (car x) (caar li) 1e-8)
                                        (equal (cadr x) (cadar li) 1e-8)
                                   )
                                   (and (equal (cadr x) (caar li) 1e-8)
                                        (equal (car x) (cadar li) 1e-8)
                                   )
                               )
                             )
                   )
                   (cdr li)
          )
        )
         (setq ll (cons (car li) ll))
      )
      (setq li (cdr li))
    )
    (foreach l ll
      (entmake (list
                 '(0 . "LINE")
                 (cons 10 (car l))
                 (cons 11 (cadr l))
                 '(62 . 1)
               )
      )
    )
                                        (setq err t)
  )
...

And this - note (setq errr 0) :
Code: [Select]
...
  ;;------------------------------------main routine ------------------------------;;

  (setq e (car (entsel "\nSelect a closed polygon :")))
                                        ;  (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
                                        ;  (vla-startundomark adoc)
  (setq #gsls_systemvar# (list "CMDECHO" "CLAYER"))
  (_svos) ;_undo begin , system variables store .
  (setq area (vlax-curve-getarea e))
  (if (< area 1000000000.0)
    (progn
      (setq scf (sqrt (/ 1000000000.5 area)))
      (alert "You must scale polyline - quitting")
      (prompt
        (strcat
          "\nYou must scale polyline with this factor to make routine compute ridges correctly : "
          (rtos scf 2 15)
        )
      )
      (prompt
        (strcat
          "\nInverse scale factor to return scaled solution back is : "
          (rtos (/ 1.0 scf) 2 15)
        )
      )
      (textscr)
      (exit)
    )
  )
  ;|
  (setq pl
  (mapcar (function cdr)
   (vl-remove-if-not (function (lambda (a) (= (car a) 10))) (entget e))))
  |;
  (setq pl (lm:entity->pointlist e) ;_for pl with arc
        pl (remove-same-pts pl 1e-6)
  ) ;_has doubles points in pl with arc
                                        (setq errr 0)
  (start pl)
  (_clos) ;_undo end , system variables reset . 
)
...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 06, 2014, 06:28:23 AM
chlh_jd, only if you have spare time... Look attached DWG... Hope I am not exaggerating...

(My posted LSP files updated and reattached in my previous post)

M.R.

[EDIT] : Attached one more...
Thanks...
Ribar , thank you for test  :-)
Although I have a better software to generate sloped roof, and this routine did take me a lot time  , but I still want to improve it  :-)

Just to mention, SOLUTION WASNT FINISHED.DWG is solvable through multiple attempts with error prompts from one of 2 versions - I think (while) 2droof.lsp...

Just uncomment these lines :

And this - note (setq err t) :

And this - note (setq errr 0) :
M.R.
Just like I've said ,if want to let it no catch any error , other word , to suit for any case (In fact, because there is precision in the calculation can not be done , I agree you that some case must be scaled , it also can be determine in the routine , after get the roof lines scaling back ) , It must give up some efficiency .
For this reason , I would like to deal crash side in the last part of the main routine  , insted of traversing each side over and over again .

I'll take some time to test your programe , but today is busy  , Sorry .
I post the V0.7 programe , it show how to solve some error in the last part and did get over the case in your post dwg .

Enjoy !
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 06, 2014, 06:55:00 AM
Never mind, chlh_jd... When you have time only then continue with coding... Take care...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 06, 2014, 10:17:35 AM
chlh_jd, I've implemented your intervention from v.7 to my code, but now complex cases are solved, but simple one isn't... Can you help... Please check "WRONG SOLUTION - ERROR - SIMPLE POLYLINE.dwg" and "SOLUTION WASNT FINISHED.dwg" posted earlier with my codes... On your animated GIF it seems that it functions...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 07, 2014, 06:02:04 AM
Try to solve it yourself , you can annotate part like {(foo) ...;_while} {(foo1} ...;_while} {(while ...;_while} to found which part catch error , and determine which function ... , set a breakpoint by vertex or side , and easy to find where is wrong .
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 08, 2014, 04:39:57 AM
I've scaled it even more and fixed scale factor to be nicer rounded number... Seems that now sometime it solves simple lwpolyline width outline and sometime it fails... But this is the best I could do... Your code remained unchanged... Here are the routines I use for now... Sorry I was been unsatisfied it passed me... Some very complex polylines can't be solved, but almost all that are decently drawn are solvable...

Thanks, chlh_jd, and sorry for my rough and wrong expectations...

Regards, all the best, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 08, 2014, 11:10:24 AM
I've scaled it even more and fixed scale factor to be nicer rounded number... Seems that now sometime it solves simple lwpolyline width outline and sometime it fails... But this is the best I could do... Your code remained unchanged... Here are the routines I use for now... Sorry I was been unsatisfied it passed me... Some very complex polylines can't be solved, but almost all that are decently drawn are solvable...

Thanks, chlh_jd, and sorry for my rough and wrong expectations...

Regards, all the best, M.R.
Ribar , you're welcome .
Never mind , every one wish everything perfect .

For scale object , you can put a prompt "Suggest use mm/inch as the min unit to creat slopedroof" , also you can add determine size of the polygon's boundingbox is suitable ? if not scaling it by in the routine , but it may also catch the precision problem . So I suggest do it by users themself .

You can try this case to find which size is suitable --- make 4 points which inside angle is close to zero or pi , and get their intersection ; the next do while loop for scale they 10~1e9,get each intersection and scale back , used deffrent color mark them , and found which is the correct limit .

Code: [Select]
(defun c:test(/ f1 f2 p0 p1 p2 p3 an1 an2 i p pa pb pc i%)
  (setq p0 (getpoint)
p1 (getpoint p0)
p2 (getpoint)
p3 (getpoint p2))
  (setq an1 (angle p0 p1)
an2 (angle p2 p3)
i 0)
  (defun f1 (p)
    (mapcar (function (lambda (x)
(* x 0.1 (expt 10 i))))
    p))
  (defun f2 (p)
    (mapcar (function (lambda (x)
(/ x (* 0.1 (expt 10 i)))))
    p))
  (repeat 9
    (setq i (1+ i))
    (setq p0 (f1 p0)
  p1 (f1 p1)
  p2 (f1 p2)
  p3 (f1 p3))
    (setq p (inters p0 p1 p2 p3 nil)
  pa (inters p0 (polar p0 an1 1.) p2 (polar p2 an2 1.) nil)
  pb (inters p0 (polar p0 an1 1e3) p2 (polar p2 an2 1e3) nil)
  pc (inters p0 (polar p0 an1 1e9) p2 (polar p2 an2 1e9) nil))
    (setq p (f2 p)
  pa (f2 pa)
  pb (f2 pb)
  pc (f2 pc))
    (setq i% 0)
    (mapcar (function (lambda (p)
(if p
  (entmake (list (cons 0 "POINT")
       (cons 10 p)
       (cons 62 (+ i (* 10 i%))))))
(setq i% (1+ i%))
))
    (list p pa pb pc))
    )
  (princ)
  )
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 10, 2014, 05:56:09 AM
My last version... Still it may find wrong solution or crash, but I think it's a little better for searching solutions - fewer crashes especially with polyline width outlines that were previously crashed... Beside of scaling it's also advisable to put polyline near WCS origin (0.0 0.0 0.0)...

So long from me, M.R.

[EDIT] : 2droof.lsp & 2droof-t.lsp updated to reflect changes in new chlh_jd's Proof_8.lsp
[EDIT] : 2droof.lsp & 2droof-t.lsp updated to reflect changes in new chlh_jd's Proof_9.lsp
[EDIT] : 2droof.lsp & 2droof-t.lsp updated to reflect changes in new chlh_jd's Proof_10.lsp
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 10, 2014, 01:47:43 PM
New version , add header and more test .
Code - Auto/Visual Lisp: [Select]
  1. ;;;-----------------------------------------------------------------------------;;;
  2. ;;;  
  3. ;;;  ---------------------------------------------------------------------------;;;
  4. ;;;  SlopedRoof                                                                 ;;;
  5. ;;;  ---------------------------------------------------------------------------;;;
  6. ;;;  function : Find the ridge lines of sloped roof                             ;;;
  7. ;;;  ---------------------------------------------------------------------------;;;
  8. ;;;  Return:                                                                    ;;;
  9. ;;;         2D -- SlopedRoof Lines / SlopedRoof Closed Polygons of  Each Side   ;;;
  10. ;;;         3D -- SlopedRoof Closed Polygons of Each face                       ;;;
  11. ;;;                          / A Union Surface                                  ;;;
  12. ;;;                          / Solids of Each face                              ;;;
  13. ;;;  ---------------------------------------------------------------------------;;;
  14. ;;;  Writen By GSLS(SS) June 2014                                               ;;;
  15. ;;;        (C)  EasyCity OptDesign Studio of Building Structures                ;;;
  16. ;;;  Email: chlh_jd@126.com        Tel:86-0592-5391029    Fax:86-0592-5391020   ;;;
  17. ;;;  ---------------------------------------------------------------------------;;;
  18. ;;;  Main thread reference :                                                    ;;;
  19. ;;;      JianGuo Li, Algorithm about generate slopedroof line from 2D polygon.  ;;;
  20. ;;;      [BC Dissertation] Hubei University in China . 2009.                   ;;;
  21. ;;;      Many Thanks to JianGuo Li !                                            ;;;
  22. ;;;  -------------------------------------------                                ;;;
  23. ;;;  JianGuo Li's article main abort references :
  24. ;;;      [5] D.T.Lee. Medial axis transformation of a planar shape [J],IEEE Trans PAM I , 1982, 4:363-369
  25. ;;;      [6] Chin F, Snoeyink J., Wang C.A. Finding the medial axis of a simple polygon in linear time
  26. ;;;           [J]. Discrete and Computational Geometry , 1999, 21:405-420
  27. ;;;      [8] Joseph O,Rourke. Computational Geometry In C [M].Second Edition. Cambridge University Press, 2004.179-181
  28. ;;;      ......
  29. ;;;  ---------------------------------------------------------------------------;;;
  30. ;;;  Version revit See Command Routine .                                        ;;;
  31. ;;;  -------------------------------------------                                ;;;
  32. ;;;  Thanks Ribar.M from http://www.theswamp.org  for do many test              ;;;
  33. ;;;  -------------------------------------------                                ;;;
  34. ;;;  Discuss website : http://www.theswamp.org/index.php?topic=41837.0          ;;;
  35. ;;;  -------------------------------------------                                ;;;
  36. ;;;  Any Advice will be welcome , Thank you !                                   ;;;
  37. ;;;  ---------------------------------------------------------------------------;;;
  38.  
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 10, 2014, 04:31:10 PM
chlh_jd, thanks for your LISP... Found little mistake - change (getangle) to (getreal) when specifying Degrees... Everything else is fine... I haven't analyzed it but it seems that it handles polyline width outlines... So with less coding it's better than mine, but there is one example where my version can handle complex polyline - after error messages and your is looping forever...

Thanks, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 13, 2014, 01:28:13 AM
chlh_jd, thanks for your LISP... Found little mistake - change (getangle) to (getreal) when specifying Degrees...
Ribar , you're welcome . Many thanks for your test .
GetAngle allow user type in specifying angle , I don't think it's a mistake unless you give some e.g. .

... Everything else is fine... I haven't analyzed it but it seems that it handles polyline width outlines
Thanks, M.R.
The width pl to ontlines routine I'v writen so long ago and no time to opt it , do you have a better version ?
... So with less coding it's better than mine, but there is one example where my version can handle complex polyline - after error messages and your is looping forever...
Can you post the error dwg ? Thanks .
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 13, 2014, 04:34:23 AM
chlh_jd, thanks for your LISP... Found little mistake - change (getangle) to (getreal) when specifying Degrees...
Ribar , you're welcome . Many thanks for your test .
GetAngle allow user type in specifying angle , I don't think it's a mistake unless you give some e.g. .
If you plan to keep (getangle), you should remove line with conversion of variable a to radians - getangle takes values in degrees and automatically assigns radians to variable... The simplest way for modifications is to change (getangle) to (getreal) and to keep everything else as it is...
... Everything else is fine... I haven't analyzed it but it seems that it handles polyline width outlines
Thanks, M.R.
The width pl to ontlines routine I'v writen so long ago and no time to opt it , do you have a better version ?
No, chlh_jd I use your version, but I believe Lee Mac has also this routine - both normal and advanced versions with elliptical arcs if polyline with arced segments...
... So with less coding it's better than mine, but there is one example where my version can handle complex polyline - after error messages and your is looping forever...
Can you post the error dwg ? Thanks .
I'll post examples, but note that I can't occupy theswamp with so many of them, still there are complex polylines that aren't solvable for now, just draw one with many vertices and go zig-zag in making segments - avoid convex shapes - concave are problematical...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 14, 2014, 08:17:58 AM
Thanks Ribar .

1. I knew getangle will return degree , I use it just for degree in the case .

2. Not all things are open source, and use it and cherish it .

3. Just like I've said , the routine can't suit for any case , what you post you can solve in yours , I'll try to slove it , but not sure I can do it .

Thank you again .

Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 14, 2014, 02:44:37 PM
Ribar , new version fix the bug your post .
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 15, 2014, 05:31:35 AM
chlh_jd, thanks for new Proof_9.lsp... Still it isn't all OK... Now complex roof... You can try to make similar examples and test... Only if you have extra time and wish to continue...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 17, 2014, 04:41:16 AM
Ribar , the error you post has been fix , see proof-10.lsp and gif , thank you .
Proof-10 , add 2d frame draw mode , it's like the frame get by voronoi method , but higher acc than voronoi .
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 17, 2014, 08:27:02 AM
Thanks, chlh_jd... Another one, only if you have wish to continue... And so on...

Many thanks, regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 19, 2014, 02:01:34 PM
Nothing special, updated LISPs posted here :

http://www.theswamp.org/index.php?topic=41837.msg522582#msg522582

Regards, M.R.

[EDIT] : There was mistake - now corrected...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 20, 2014, 12:10:35 PM
I've discovered that my 2droof3d.lsp had one lack - when start/end vertex of LWPL is the same than it failed... So I decided to post update and 2 lisps for converting arced LWPL to segmented one and lisp to execute before 2droof3d on segmented LWPL that is lisp that cleans double vertices that are the same... If everything is prepared (arced LWPL), and chlh_jd's 3D option failed, you should try with pline-arcs-seg.lsp, then clpls.lsp, then my posted 2droof.lsp (see previous post) and when solutions was find and correct, then apply here posted 2droof3d.lsp (this one is update - it operates and with LWPL with the same start/end vertex)...

Regards, M.R.

[EDIT : ] The topic that may also be of interest and that has some relation with posted routines - look into this link :
http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-to-convert-polyline-containing-tessellated-segments-to-true/m-p/5345845/highlight/true#M326908
[/EDIT]

[EDIT : ] "pline-arcs-seg.lsp" has small issue ab duplicate vertices at start/end of some segments... To fix this problem look here :
http://www.cadtutor.net/forum/showthread.php?89479-convert-arcs-in-polyline-to-small-lines&p=#7
[/EDIT]
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 24, 2014, 01:08:48 PM
Ribar , thank you for your constant test  :-)
The bug in your post , has been fixed , see proof-10a.lsp and gif .
Though 3D to see 2D method , the program will  be more concise and efficient . Recently I have little time ,and I think it must add the exist island case , I'll try to do it later ... .
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 24, 2014, 03:26:04 PM
Thanks, chlh_jd... Now updated 2droof.lsp to reflect changes made in Proof_10a.lsp...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 25, 2014, 05:32:09 AM
Again, new problem... chlh_jd, only if you wish and have time...

[EDIT] : I've modified 2droof.lsp to capture crash... You can download it also here - see attachment...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 26, 2014, 11:43:01 AM
If it could help you, your previous version haven't failed on _ROOF-PROBLEM!!!.DWG and now it fails... Also, I've solved manually what should you get with my previous post...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 26, 2014, 12:05:39 PM
Thanks Ribar .
The bug you post has been fixed . See proof-11.lsp and the gif .
In this version , I do some simplifying -- dump the last while loop and foo2 , greatly reduce codes of the main routine .
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 26, 2014, 01:35:01 PM
Hi, Ribar
I think the '_ROOF - COMPLEX SOLUTION LOOPS - MANUAL SOLUTION' isn't correct , you can check them by offset method .
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 26, 2014, 02:18:35 PM
Yes, you're right chlh_jd... But by offset tests, if you check it in 3D it will be correct... Never mind that, I think your last Proof_11.lsp is seriously the best... If I may conclude from my side, all my tests passed, so I'll leave to others if some revision is to be made... If I may say, I thank you very much for all your efforts to bring this topic to finish in appropriate way... This challenge inspired me to try to achieve and to improve my programming skills although I learned lisp by reading www on my own and I am not a programmer by a profession... I really want to thank to all the people I may disrespect in my past and from whom I've also learned lots of things... You are certainly one among them chlh_jd, and as I see now old topic have become actual and I also want to thank to Mr. Lee Mac for his unselfish will to help this community to be better... I already forgot ab topic where Lee asked why I removed his LM: prefix in his subfunctions and I explained what was true from one side of view - my subjective opinion... Objectively, he helped many people to understand better and improve programming skills and by that I am very thankful to him... I appointed that, but as I use lots of lisping, my library isn't the most appropriate for posting on the forums, many things have been modified over time and may not print contributions of all the people that deserve to be mentioned... But certainly code that was posted in the past has it's beneficial purpose and certainly wasn't posted to insult anyone... Lee felt that his contribution wasn't mentioned, but in fact this is the most apparent thing that is obviously present in almost every topic and code you can find on www... So I paid contribution to people that have no habit to leave any sign and are certainly good and skillful programmers like Lee is... Among them I see you chlh_jd, and many of them are still invisible to me... So in fact I want to thank to all of them and also Autodesk people that made their software so popular and practical...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 26, 2014, 08:42:12 PM
Yes, you're right chlh_jd... But by offset tests, if you check it in 3D it will be correct...
One 2D closed curve reflect one best 3D roof , but it can be other 3D roofs which also can drainage .
Never mind that, I think your last Proof_11.lsp is seriously the best... If I may conclude from my side, all my tests passed, so I'll leave to others if some revision is to be made... If I may say, I thank you very much for all your efforts to bring this topic to finish in appropriate way...
...You are certainly one among them chlh_jd,
Thank you , Ribar  :-)
and as I see now old topic have become actual and I also want to thank to Mr. Lee Mac for his unselfish ...
I'v not seen dear and clever Lee Mac , a long time . I think he has a broad mind and forgive some one .
I do wish the disputes in Evgeniy's topic  is a start for some one , and also is a end for some one ---- Respect the labor of others is so important !

For me , If I made this mistake , I will send a Email to apologize .

In my mind , we shoud not only add the code's worker , but also shoud add the ideas / algorithms / mathematical principles, etc. References .
This is a better way to standing on the shoulders of Giants . It's only my personal views .

Thank you .

Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: CAB on June 27, 2014, 07:42:00 AM
I must say that when I first started Lisping back in 2003 I did not understand
the ownership of code. When collecting routines & subroutines I did not collect
the link to the source. This became a problem when I wanted to use the routine
in a posting of my own because often the author did not include a proper header
only a prefix on the defun name. The mistake I made was to include the
subroutine in my routine which I placed a header implying ownership. Fortunately
someone pointed out the error in my thinking (thank you MP) & I made an effort
to add headers to those sub-functions and links back to the source. This is like
foot notes in an article or book. So to omit these references is unthinking and
doing it regularly is rude behavior. I still have code floating around out in
cyberspace that I would like to correct but it is too late for most.
Once it leaves here you can not track it all down.

But now I make sure that any code I barrow has a reference to who or at least
where I obtained it so as not to IMPLY that I was the creator of the code. There
is a lot of effort and many late night study sessions that went into the ability
to create these routines that are so good that we want to reuse them rather than
create one of our own.

So honor that effort by including the proper header and or link back to the source.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on June 27, 2014, 04:33:09 PM
Thanks Alan share your experiences and feelings  . I did this mistake too .

New version for reduce  codes ,  see proof-12.lsp

Due to personal time, this topic for me had to come to an end . It can be add the Island case .  I wish  someone can do it .

Code - Auto/Visual Lisp: [Select]
  1. ;;;-----------------------------------------------------------------------------;;;
  2. ;;;  
  3. ;;;  ---------------------------------------------------------------------------;;;
  4. ;;;  SlopedRoof                                                                 ;;;
  5. ;;;  ---------------------------------------------------------------------------;;;
  6. ;;;  function : Find the ridge lines of sloped roof                             ;;;
  7. ;;;  ---------------------------------------------------------------------------;;;
  8. ;;;  Return:                                                                    ;;;
  9. ;;;         2D -- SlopedRoof Lines / SlopedRoof Closed Polygons of  Each Side   ;;;
  10. ;;;                   / Frame lines, like get by voronoi method, more accurate  ;;;
  11. ;;;         3D -- SlopedRoof Closed Polygons of Each face                       ;;;
  12. ;;;                          / A Union Surface                                  ;;;
  13. ;;;                          / Solids of Each face                              ;;;
  14. ;;;  ---------------------------------------------------------------------------;;;
  15. ;;;  Writen By GSLS(SS) June 2014                                               ;;;
  16. ;;;        (C)  EasyCity OptDesign Studio of Building Structures                ;;;
  17. ;;;  Email: chlh_jd@126.com        Tel:86-592-5391029    Fax:86-592-5391020     ;;;
  18. ;;;  ---------------------------------------------------------------------------;;;
  19. ;;;  Main thread reference :                                                    ;;;
  20. ;;;      JianGuo Li, Algorithm about generate slopedroof line from 2D polygon.  ;;;
  21. ;;;      &#65339;BC Dissertation&#65341;Hubei University in China . 2009.                   ;;;
  22. ;;;      Many Thanks to JianGuo Li !                                            ;;;
  23. ;;;  -------------------------------------------                                ;;;
  24. ;;;  JianGuo Li's article main abort references :
  25. ;;;      [5] D.T.Lee. Medial axis transformation of a planar shape [J],IEEE Trans  
  26. ;;           PAM I , 1982, 4:363-369                                              
  27. ;;;      [6] Chin F, Snoeyink J., Wang C.A. Finding the medial axis of a simple    
  28. ;;;          polygon in linear time [J]. Discrete and Computational Geometry ,    
  29. ;;;          1999, 21:405-420                                                      
  30. ;;;      [8] Joseph O,Rourke. Computational Geometry In C [M].Second Edition.      
  31. ;;;          Cambridge University Press, 2004.179-181                              
  32. ;;;      ......                                  
  33. ;;;  ---------------------------------------------------------------------------;;;
  34. ;;;  Version revit See Command Routine .                                        ;;;
  35. ;;;  -------------------------------------------                                ;;;
  36. ;;;  Thanks Ribar.M from http://www.theswamp.org  for do many test              ;;;
  37. ;;;  Thanks Lee Mac, Jueao Sword, Charles Alan Butler for their Wonderful functions
  38. ;;;  -------------------------------------------                                ;;;
  39. ;;;  Discuss website : http://www.theswamp.org/index.php?topic=41837.0          ;;;
  40. ;;;  -------------------------------------------                                ;;;
  41. ;;;  Future Expansion : Support Island Case !                                   ;;;
  42. ;;;  -------------------------------------------                                ;;;
  43. ;;;  Any Advice will be welcome , Thank you !                                   ;;;
  44. ;;;  ---------------------------------------------------------------------------;;;
  45. ;;;  Statement :
  46. ;;;   1. When you use this program, all the consequences will be borne by yourself,
  47. ;;;      Please repeatedly check its correctness .
  48. ;;;   2. This program is only for exchange and information, not for commercial use.
  49. ;;;  ---------------------------------------------------------------------------;;;
  50.  
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: RAIN CODE on June 28, 2014, 03:39:45 AM
I must say that when I first started Lisping back in 2003 I did not understand
the ownership of code. When collecting routines & subroutines I did not collect
the link to the source. This became a problem when I wanted to use the routine
in a posting of my own because often the author did not include a proper header
only a prefix on the defun name. The mistake I made was to include the
subroutine in my routine which I placed a header implying ownership. Fortunately
someone pointed out the error in my thinking (thank you MP) & I made an effort
to add headers to those sub-functions and links back to the source. This is like
foot notes in an article or book. So to omit these references is unthinking and
doing it regularly is rude behavior. I still have code floating around out in
cyberspace that I would like to correct but it is too late for most.
Once it leaves here you can not track it all down.

But now I make sure that any code I barrow has a reference to who or at least
where I obtained it so as not to IMPLY that I was the creator of the code. There
is a lot of effort and many late night study sessions that went into the ability
to create these routines that are so good that we want to reuse them rather than
create one of our own.

So honor that effort by including the proper header and or link back to the source.

I have been waiting for someone to say this...I echoed what CAB is saying.
The author have the right to demand his/her name be in the header and stay there with the lisp.

People like to copy others people lisp and then remove the author's name and they just use the lisp
without having to crack their head to write it. If you want to use the lisp at least leave the author's name there
and if the function name has the author initial please give the author the honor and respect and leave it there.

It is only few characters long no point of removing it to reduce to code size. If you are afraid of people saying that
why most of the subroutines are written by another person, then you should crack your head and write it yourself.

Becos it save you the time to write it then should give credits to the person who spent time to write it



Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: RAIN CODE on June 28, 2014, 12:19:58 PM
I want to correct one thing - my view is different from CAB.

I apologize to CAB for that.

Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 05, 2014, 06:32:24 PM
Here are my latest codes referencing this topic...

Video can be found at Youtube...

http://youtu.be/FapqxIqQRv8

In attachment are all relevant routines used in this video...

Regards, M.R.

P.S. In some situations, 2droof-final.lsp may fail to produce correct and desired results, so be careful... Suggested is not to use sr.lsp until 2d solution made by 2droof-final.lsp is adequate (sr.lsp is using the same algorithm)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on August 05, 2014, 10:03:19 PM
Here are my latest codes referencing this topic...

Video can be found at Youtube...

http://youtu.be/FapqxIqQRv8

In attachment are all relevant routines used in this video...

Regards, M.R.

P.S. In some situations, 2droof-final.lsp may fail to produce correct and desired results, so be careful... Suggested is not to use sr.lsp until 2d solution made by 2droof-final.lsp is adequate (sr.lsp is using the same algorithm)
Nice work Ribar  :-)
Thank you for keep the head .

Is there problem in my last post version ?
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 07, 2014, 06:04:19 AM
Nice work Ribar  :-)
Thank you for keep the head .

Is there problem in my last post version ?

Check my codes and compare with your - sr.lsp

2droof3d.lsp is updated... Noticed that when points are near to each other and roof is large, routine must zoom to vertices sequences and then zoom previous to view before routine started to operate... It's strongly suggested that complete roof should be on visible screen so that (command "_.zoom" "_w" vert1 vert2) could operate correctly... Someone downloaded version where I forgot to include every vertices sequence - zooming was put inside if condition... I apologize for this it was late yesterday and my concentration was bad... Now fixed...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 10, 2014, 10:49:02 AM
2droof-final.lsp and sr.lsp have been changed... Now should work better and almost every time should output result weather it's good or bad... Only thing is that with complex roofs you should wait for a while... But I did what I could to make it fast enough...

Hope you'll like it...

Regards, M.R.
 :-)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on July 29, 2017, 04:15:17 AM
So it's been 3 years I haven't revised this interesting challenge... But I've found some time to revive good old memories... Basically I've written shortened versions, but it still has some issues - 3d solid - surfsculpting is relatively good, but I am though interested if someone can give boost in version with 3d lines... So here are my latest short attempts :

For SURFSCULPT into 3D SOLID ROOF :
Code - Auto/Visual Lisp: [Select]
  1. (defun c:roof-mr ( / *error* unit v^v clockwise-lw gravityceter3d adoc s slope lw a vl tl ucsf regs tll ls lsl ip ipl 3df c p1 p2 p3 p4 scf ss )
  2.  
  3.  
  4.   (defun *error* ( m )
  5.     (if ucsf
  6.       (vl-cmdf "_.UCS" "_P")
  7.     )
  8.     (if adoc
  9.       (vla-endundomark adoc)
  10.     )
  11.     (if m
  12.       (prompt m)
  13.     )
  14.     (princ)
  15.   )
  16.  
  17.   (defun unit ( v )
  18.     (if (not (equal v '(0.0 0.0 0.0) 1e-8))
  19.       (mapcar (function (lambda ( x ) (/ x (distance '(0.0 0.0 0.0) v)))) v)
  20.     )
  21.   )
  22.  
  23.   (defun v^v ( u v )
  24.     (list
  25.       (- (* (cadr u) (caddr v)) (* (caddr u) (cadr v)))
  26.       (- (* (caddr u) (car v)) (* (car u) (caddr v)))
  27.       (- (* (car u) (cadr v)) (* (cadr u) (car v)))
  28.     )
  29.   )
  30.  
  31.   (defun clockwise-lw ( lw / minpt maxpt p1 p2 p3 p4 pmax )
  32.     (vla-getboundingbox (vlax-ename->vla-object lw) 'minpt 'maxpt)
  33.     (mapcar (function set) '(minpt maxpt) (mapcar (function safearray-value) (list minpt maxpt)))
  34.     (setq p1 minpt p2 (list (car maxpt) (cadr minpt)) p3 maxpt p4 (list (car minpt) (cadr maxpt)))
  35.     (setq pmax (max p1 p2 p3 p4))
  36.     (cond
  37.       ( (and (= pmax p1) (> p2 p4))
  38.         t
  39.       )
  40.       ( (and (= pmax p2) (> p3 p1))
  41.         t
  42.       )
  43.       ( (and (= pmax p3) (> p4 p2))
  44.         t
  45.       )
  46.       ( (and (= pmax p4) (> p1 p3))
  47.         t
  48.       )
  49.       ( t nil )
  50.     )
  51.   )
  52.  
  53.   (defun gravitycenter3d ( p1 p2 p3 / mid p12 p23 p31 c )
  54.  
  55.     (defun mid ( p1 p2 )
  56.       (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) p1 p2)
  57.     )
  58.  
  59.     (setq p12 (mid p1 p2))
  60.     (setq p23 (mid p2 p3))
  61.     (setq p31 (mid p3 p1))
  62.     (setq c (inters p12 p3 p1 p23))
  63.  
  64.     c
  65.  
  66.   )
  67.  
  68.   (prompt "\nPick closed LWPOLYLINE POLYGON...")
  69.   (setq s (ssget "_+.:E:S" '((0 . "LWPOLYLINE") (-4 . "<or") (70 . 1) (70 . 129) (-4 . "or>") (-4 . "<not") (-4 . "<>") (42 . 0.0) (-4 . "not>"))))
  70.   (while (not s)
  71.     (prompt "\nMissed or picked wrong entity type...")
  72.     (setq s (ssget "_+.:E:S" '((0 . "LWPOLYLINE") (-4 . "<or") (70 . 1) (70 . 129) (-4 . "or>") (-4 . "<not") (-4 . "<>") (42 . 0.0) (-4 . "not>"))))
  73.   )
  74.   (initget 7)
  75.   (setq slope (getreal "\nSpecify slope angle in decimal degrees : "))
  76.   (setq lw (ssname s 0))
  77.   (setq vl (mapcar (function (lambda ( x ) (trans (list (car x) (cadr x) (cdr (assoc 38 (entget lw)))) lw 0))) (mapcar (function cdr) (vl-remove-if (function (lambda ( x ) (/= (car x) 10))) (entget lw)))))
  78.   (setq tl (mapcar (function (lambda ( a b ) (list a b))) vl (cdr (reverse (cons (car vl) (reverse vl))))))
  79.   (if (clockwise-lw lw)
  80.     (progn
  81.       (setq vl (reverse vl))
  82.       (setq tl (mapcar (function (lambda ( a b ) (list a b))) vl (cdr (reverse (cons (car vl) (reverse vl))))))
  83.     )
  84.   )
  85.   (vla-copy (vlax-ename->vla-object lw))
  86.   (vl-cmdf "_.REGION" (entlast))
  87.   (while (< 0 (getvar 'cmdactive)) (vl-cmdf ""))
  88.   (setq regs (cons (entlast) regs))
  89.   (vl-cmdf "_.PEDIT" lw "_W" 0.1)
  90.   (while (< 0 (getvar 'cmdactive)) (vl-cmdf ""))
  91.   (vl-cmdf "_.CONVTOSURFACE" lw "")
  92.   (while (< 0 (getvar 'cmdactive)) (vl-cmdf ""))
  93.   (setq regs (cons (entlast) regs))
  94.   (if (= (getvar 'worlducs) 0)
  95.     (progn
  96.       (vl-cmdf "_.UCS" "_W")
  97.       (setq ucsf t)
  98.     )
  99.   )
  100.   (foreach tt tl
  101.     (vl-cmdf "_.UCS" "_3P" "_non" (car tt) "_non" (cadr tt) "")
  102.     (vl-cmdf "_.UCS" "_X" slope)
  103.     (setq tll (cons (list tt (trans '(0.0 0.0 1.0) 1 0 t)) tll))
  104.     (vl-cmdf "_.UCS" "_P")
  105.     (vl-cmdf "_.UCS" "_P")
  106.   )
  107.   (foreach ttt tll
  108.     (foreach tttt (vl-remove ttt tll)
  109.       (if (equal (- (caddr (trans (caar tttt) 0 (cadr tttt))) (caddr (trans (caar ttt) 0 (cadr tttt)))) 0.0 1e-6)
  110.         (setq ls (list (caar ttt) (unit (v^v (cadr ttt) (cadr tttt)))) lsl (cons ls lsl))
  111.       )
  112.     )
  113.   )
  114.   (foreach ls lsl
  115.     (foreach lss (vl-remove ls lsl)
  116.       (if (setq ip (inters (car ls) (mapcar (function +) (car ls) (cadr ls)) (car lss) (mapcar (function +) (car lss) (cadr lss)) nil))
  117.         (if (not (vl-member-if (function (lambda ( x ) (equal ip x 1e-6))) ipl))
  118.           (progn
  119.             (setq ipl (cons ip ipl))
  120.             (setq 3df (entmakex (list '(0 . "3DFACE") (cons 10 ip) (cons 11 (car ls)) (cons 12 (car lss)) (cons 13 ip))))
  121.             (setq p1 ip)
  122.             (setq p2 (car ls))
  123.             (setq p3 (car lss))
  124.             (setq p4 ip)
  125.             (cond
  126.               ( (equal p1 p2 1e-8)
  127.                 (setq p1 p2 p2 p3 p3 p4)
  128.               )
  129.               ( (equal p2 p3 1e-8)
  130.                 (setq p1 p1 p2 p2 p3 p4)
  131.               )
  132.               ( (equal p3 p4 1e-8)
  133.                 (setq p1 p1 p2 p2 p3 p3)
  134.               )
  135.               ( (equal p4 p1 1e-8)
  136.                 (setq p1 p1 p2 p2 p3 p3)
  137.               )
  138.             )
  139.             (setq c (gravitycenter3d p1 p2 p3))
  140.             (vl-cmdf "_.SCALE" 3df "" "_non" c 1.01)
  141.             (vl-cmdf "_.REGION" 3df)
  142.             (while (< 0 (getvar 'cmdactive)) (vl-cmdf ""))
  143.             (setq scf (/ a (vla-get-area (vlax-ename->vla-object (entlast)))))
  144.             (if (< scf 1.0)
  145.               (setq scf 2.0)
  146.             )
  147.             (vl-cmdf "_.SCALE" (entlast) "" "_non" ip scf)
  148.             (setq regs (cons (entlast) regs))
  149.           )
  150.         )
  151.       )
  152.     )
  153.   )
  154.   (setq ss (ssadd))
  155.   (foreach reg regs
  156.     (ssadd reg ss)
  157.   )
  158.   (vl-cmdf "_.SURFSCULPT" ss "")
  159.   (*error* nil)
  160. )
  161.  

And for 3D LINEs - I want to share this as it isn't so reliable and maybe, and I said just maybe someone who wants to play with it could improve it further more - I tried several times, but unsuccessful... The problem is that when you manually solve roofs you start from all around pline vertices and solve till first touch is detected and then you continue also from all around all until last segment is connected in roof apex point... Computer can't imitate this human action and it starts from one side and continue all up to the end last side - and if result is good then it's pure luck...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:roof-mr ( / *error* unique unit v^v clockwise-lw adoc s slope lw vl tl tll ls lls lss lsl lslx lsll ip ipl lil tst ipx k kk n ucsf )
  2.  
  3.  
  4.   (defun *error* ( m )
  5.     (if ucsf
  6.       (vl-cmdf "_.UCS" "_P")
  7.     )
  8.     (if adoc
  9.       (vla-endundomark adoc)
  10.     )
  11.     (if m
  12.       (prompt m)
  13.     )
  14.     (princ)
  15.   )
  16.  
  17.   (defun unique ( l )
  18.     (if l
  19.       (cons (car l) (unique (vl-remove-if (function (lambda ( x ) (equal (car l) x 1e-6))) l)))
  20.     )
  21.   )
  22.  
  23.   (defun unit ( v )
  24.     (if (not (equal v '(0.0 0.0 0.0) 1e-8))
  25.       (mapcar (function (lambda ( x ) (/ x (distance '(0.0 0.0 0.0) v)))) v)
  26.     )
  27.   )
  28.  
  29.   (defun v^v ( u v )
  30.     (list
  31.       (- (* (cadr u) (caddr v)) (* (caddr u) (cadr v)))
  32.       (- (* (caddr u) (car v)) (* (car u) (caddr v)))
  33.       (- (* (car u) (cadr v)) (* (cadr u) (car v)))
  34.     )
  35.   )
  36.  
  37.   (defun clockwise-lw ( lw / minpt maxpt p1 p2 p3 p4 pmax )
  38.     (vla-getboundingbox (vlax-ename->vla-object lw) 'minpt 'maxpt)
  39.     (mapcar (function set) '(minpt maxpt) (mapcar (function safearray-value) (list minpt maxpt)))
  40.     (setq p1 minpt p2 (list (car maxpt) (cadr minpt)) p3 maxpt p4 (list (car minpt) (cadr maxpt)))
  41.     (setq pmax (max p1 p2 p3 p4))
  42.     (cond
  43.       ( (and (= pmax p1) (> p2 p4))
  44.         t
  45.       )
  46.       ( (and (= pmax p2) (> p3 p1))
  47.         t
  48.       )
  49.       ( (and (= pmax p3) (> p4 p2))
  50.         t
  51.       )
  52.       ( (and (= pmax p4) (> p1 p3))
  53.         t
  54.       )
  55.       ( t nil )
  56.     )
  57.   )
  58.  
  59.   (prompt "\nPick closed LWPOLYLINE POLYGON...")
  60.   (setq s (ssget "_+.:E:S" '((0 . "LWPOLYLINE") (-4 . "<or") (70 . 1) (70 . 129) (-4 . "or>") (-4 . "<not") (-4 . "<>") (42 . 0.0) (-4 . "not>"))))
  61.   (while (not s)
  62.     (prompt "\nMissed or picked wrong entity type...")
  63.     (setq s (ssget "_+.:E:S" '((0 . "LWPOLYLINE") (-4 . "<or") (70 . 1) (70 . 129) (-4 . "or>") (-4 . "<not") (-4 . "<>") (42 . 0.0) (-4 . "not>"))))
  64.   )
  65.   (initget 7)
  66.   (setq slope (getreal "\nSpecify slope angle in decimal degrees : "))
  67.   (setq lw (ssname s 0))
  68.   (setq vl (mapcar (function (lambda ( x ) (trans (list (car x) (cadr x) (cdr (assoc 38 (entget lw)))) lw 0))) (mapcar (function cdr) (vl-remove-if (function (lambda ( x ) (/= (car x) 10))) (entget lw)))))
  69.   (setq tl (mapcar (function (lambda ( a b ) (list a b))) vl (cdr (reverse (cons (car vl) (reverse vl))))))
  70.   (if (clockwise-lw lw)
  71.     (progn
  72.       (setq vl (reverse vl))
  73.       (setq tl (mapcar (function (lambda ( a b ) (list a b))) vl (cdr (reverse (cons (car vl) (reverse vl))))))
  74.     )
  75.   )
  76.   (if (= (getvar 'worlducs) 0)
  77.     (progn
  78.       (vl-cmdf "_.UCS" "_W")
  79.       (setq ucsf t)
  80.     )
  81.   )
  82.   (foreach tt tl
  83.     (vl-cmdf "_.UCS" "_3P" "_non" (car tt) "_non" (cadr tt) "")
  84.     (vl-cmdf "_.UCS" "_X" slope)
  85.     (setq tll (cons (list tt (trans '(0.0 0.0 1.0) 1 0 t)) tll))
  86.     (vl-cmdf "_.UCS" "_P")
  87.     (vl-cmdf "_.UCS" "_P")
  88.   )
  89.   (foreach ttt tll
  90.     (foreach tttt (vl-remove ttt tll)
  91.       (if (equal (- (caddr (trans (caar tttt) 0 (cadr tttt))) (caddr (trans (caar ttt) 0 (cadr tttt)))) 0.0 1e-6)
  92.         (setq ls (list (caar ttt) (unit (v^v (cadr ttt) (cadr tttt)))) lsl (cons ls lsl))
  93.       )
  94.     )
  95.   )
  96.   (foreach ls lsl
  97.     (foreach lss (vl-remove ls lsl)
  98.       (if (setq ip (inters (car ls) (mapcar (function +) (car ls) (cadr ls)) (car lss) (mapcar (function +) (car lss) (cadr lss)) nil))
  99.         (setq ipl (cons ip ipl))
  100.       )
  101.     )
  102.   )
  103.   (while (and lsl (not (equal lsl lsll 1e-6)))
  104.     (setq lsll lsl k -1)
  105.     (while (and (setq k (1+ k)) (< k (length lsl)))
  106.       (setq ls (nth k lsl) kk -1)
  107.       (while (and (setq kk (1+ kk)) (< kk (1- (length lsl))))
  108.         (setq lss (nth kk (vl-remove ls lsl)))
  109.         (if (setq ip (inters (car ls) (mapcar (function +) (car ls) (cadr ls)) (car lss) (mapcar (function +) (car lss) (cadr lss)) nil))
  110.           (if (and (> (caddr ip) 0.0) (not (vl-some (function (lambda ( x ) (or (equal (distance (car ls) ip) (+ (distance (car ls) x) (distance x ip)) 1e-6) (equal (distance (car lss) ip) (+ (distance (car lss) x) (distance x ip)) 1e-6)))) (vl-remove-if (function (lambda ( x ) (or (equal x ip 1e-6) (equal x (car ls) 1e-6) (equal x (car lss) 1e-6)))) ipl))))
  111.             (progn
  112.               (setq tst nil)
  113.               (foreach lsx lsl
  114.                 (foreach lssx (vl-remove lsx lsl)
  115.                   (if (setq ipx (inters (car lsx) (mapcar (function +) (car lsx) (cadr lsx)) (car lssx) (mapcar (function +) (car lssx) (cadr lssx)) nil))
  116.                     (if (and (> (caddr ipx) 0.0) (not (equal ipx ip 1e-6)) (or (equal (distance (car lsx) ip) (+ (distance (car lsx) ipx) (distance ipx ip)) 1e-6) (equal (distance (car lssx) ip) (+ (distance (car lssx) ipx) (distance ipx ip)) 1e-6)))
  117.                       (setq tst t)
  118.                     )
  119.                   )
  120.                 )
  121.               )
  122.               (if (null tst)
  123.                 (progn
  124.                   (if (and (not (equal (car ls) ip 1e-6)) (not (or (vl-member-if (function (lambda ( x ) (equal x (list (car ls) ip) 1e-6))) lil) (vl-member-if (function (lambda ( x ) (equal x (list ip (car ls)) 1e-6))) lil))))
  125.                     (progn
  126.                       (setq lil (cons (list (car ls) ip) lil))
  127.                       (entmake (list '(0 . "LINE") (cons 10 (car ls)) (cons 11 ip)))
  128.                     )
  129.                   )
  130.                   (if (and (not (equal (car lss) ip 1e-6)) (not (or (vl-member-if (function (lambda ( x ) (equal x (list (car lss) ip) 1e-6))) lil) (vl-member-if (function (lambda ( x ) (equal x (list ip (car lss)) 1e-6))) lil))))
  131.                     (progn
  132.                       (setq lil (cons (list (car lss) ip) lil))
  133.                       (entmake (list '(0 . "LINE") (cons 10 (car lss)) (cons 11 ip)))
  134.                     )
  135.                   )
  136.                   (setq lsl (vl-remove ls lsl) lsl (vl-remove lss lsl))
  137.                   (setq lslx nil)
  138.                   (if (setq n (unit (v^v (mapcar (function -) ip (car ls)) (mapcar (function -) ip (car lss)))))
  139.                     (setq tll (vl-remove-if (function (lambda ( x ) (or (equal n (cadr x) 1e-6) (equal (mapcar (function -) n) (cadr x) 1e-6)))) tll))
  140.                   )
  141.                   (foreach ttt tll
  142.                     (foreach tttt (vl-remove ttt tll)
  143.                       (if
  144.                         (and
  145.                           (equal (- (caddr (trans ip 0 (cadr ttt))) (caddr (trans (caar ttt) 0 (cadr ttt)))) 0.0 1e-6)
  146.                           (equal (- (caddr (trans ip 0 (cadr tttt))) (caddr (trans (caar tttt) 0 (cadr tttt)))) 0.0 1e-6)
  147.                         )
  148.                         (setq lls (list ip (unit (v^v (cadr ttt) (cadr tttt)))) lslx (cons lls lslx))
  149.                       )
  150.                     )
  151.                   )
  152.                   ;|
  153.                   (foreach ttt tll
  154.                     (foreach tttt (vl-remove ttt tll)
  155.                       (cond
  156.                         ( (equal (- (caddr (trans ip 0 (cadr ttt))) (caddr (trans (caar ttt) 0 (cadr ttt)))) 0.0 1e-6)
  157.                           (if (equal (- (caddr (trans ip 0 (cadr tttt))) (caddr (trans (caar tttt) 0 (cadr tttt)))) 0.0 1e-6)
  158.                             (setq lls (list ip (unit (v^v (cadr ttt) (cadr tttt)))) lslx (cons lls lslx))
  159.                             (setq lls (list ip (unit (v^v (cadr ttt) n))) lslx (cons lls lslx))
  160.                           )
  161.                         )
  162.                         ( (equal (- (caddr (trans ip 0 (cadr tttt))) (caddr (trans (caar tttt) 0 (cadr tttt)))) 0.0 1e-6)
  163.                           (if (equal (- (caddr (trans ip 0 (cadr ttt))) (caddr (trans (caar ttt) 0 (cadr ttt)))) 0.0 1e-6)
  164.                             (setq lls (list ip (unit (v^v (cadr tttt) (cadr ttt)))) lslx (cons lls lslx))
  165.                             (setq lls (list ip (unit (v^v (cadr tttt) n))) lslx (cons lls lslx))
  166.                           )
  167.                         )
  168.                       )
  169.                     )
  170.                   )
  171.                   |;
  172.                   (setq lslx (unique lslx))
  173.                   (setq lsl (append lslx lsl))
  174.                 )
  175.               )
  176.             )
  177.           )
  178.           (if (or (equal (unit (mapcar (function -) (car lss) (car ls))) (cadr ls) 1e-6) (equal (unit (mapcar (function -) (car lss) (car ls))) (mapcar (function -) (cadr ls)) 1e-6))
  179.             (progn
  180.               (if (and (not (equal (car ls) (car lss) 1e-6)) (not (or (vl-member-if (function (lambda ( x ) (equal x (list (car ls) (car lss)) 1e-6))) lil) (vl-member-if (function (lambda ( x ) (equal x (list (car lss) (car ls)) 1e-6))) lil))))
  181.                 (progn
  182.                   (setq lil (cons (list (car ls) (car lss)) lil))
  183.                   (entmake (list '(0 . "LINE") (cons 10 (car ls)) (cons 11 (car lss))))
  184.                 )
  185.               )
  186.               (setq lsl (vl-remove ls lsl) lsl (vl-remove lss lsl))
  187.             )
  188.           )
  189.         )
  190.       )
  191.     )
  192.   )
  193.   (*error* nil)
  194. )
  195.  

Regards, M.R.
 :-)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on July 30, 2017, 08:19:31 AM
Just a little bit improved and little bigger code - this is just for 3D LINEs version as I am interested in that just...

Code: [Select]
Code removed due to the fact that I solved this issue and it's relatively faster then it was and I am satisfied with that now...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 01, 2017, 04:48:59 PM
Here is 3dlines version that should do correctly... It's ab 20K and is core routine on which you can build 3dsolid version for which you'll need some additional subs and like I did use (break_with sub from CAB and overkill command - now not sure ab that) and region command and finally SURFSCULPT...

Code: [Select]
Code removed due to the lack of interest... You can PM on ribarm@gmail.com if you are interested...
M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 03, 2017, 03:57:26 PM
There is this game (look into gif)... But it's only for convex polygons, the problem are concave ones... When solution is found correctly with 3D LINES, it's relatively easy to make 3D SOLID from it... For future note, developing general routine that works for both cases with lines in 3D SPACE is IMO the best choice... I tried to add some codes that lead following that path and I wish you success in your study and coding...

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: HasanCAD on August 06, 2017, 04:17:36 AM
WOW
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 08, 2017, 08:40:39 AM
WOW

I've written just another lisp (25KB (3dlines) core + 10KB - 3dsolid version - 39 KB total with islands) for convex+concave polygon 3dsolid roofs... And it uses modelling operations with final SURFSCULPT... I'll post gif showing it in action and DWG with ultimate test for those developers that have some alternative which ch_lhjd's sr.lsp can't handle, and also EXTRUDE Taper => SOLIDEDIT (EXTRUDE or MOVE) top face upwards built in CAD can't handle correctly, but for mine version solved it for ab 875 sec, but still it didn't managed to solve complete roof solution, so I'll post also what it did solve (3dlines)...

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 08, 2017, 11:32:19 AM
I forgot to show that it also works with porches...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 12, 2017, 03:21:34 PM
I've speed up my routine (version) and I've made the one that works with islands... Just to see that it's very possible, I'll attach gif...

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 14, 2017, 01:40:31 PM
Another demonstration on my slow PC (using A2014 on Win7)... BTW. A2017 have some sort of bug and can't do it directly - you must save DWG and reopen after cubes are made... IMO it's reasonably fast for this task...

Regards...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 16, 2017, 08:53:53 AM
I've updated my last posted animated gif... Added lower 3DSOLIDs parts around cubes-objects... I think that it's now prettier and theswamp managed to upload gif in 640x512 resolution so now it little bigger (better)...

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 05, 2017, 06:19:13 AM
I've changed animated gif from here :
https://www.theswamp.org/index.php?topic=41837.msg580469#msg580469

And attached DWGs with ultimate test on that same post... Also I've removed codes as they were not complete, just working examples...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 30, 2018, 08:13:22 AM
@chlhjd,

I revived this topic as I solved ultimate test... I did computation - on my PC ab 2 hours... Some central finishing 3d lines were missing, so I manually reconstructed solution and finally I gathered 3d lines into 3d solid... Then I isolated only polyline and applied 2droof-final.lsp - your version and although it solved it quickly, some things are different than with correct 3d solid... Observe in top view differences - 2d lines are red color... You can also check OFFSET command to see where PC - CAD is making mistakes - the part that was reconstructed - somewhere in the middle - top apex, offset is mistaking... So this is just for your spare time - the code could be improved further more, but I don't quite understand it as it's tough code, so if you want to do it you're welcome... In attachment is my DWG with ultimate test... After this, if it's corrected, maybe AutoDesk should check their OFFSET command for improvement... It is buggy on many things (2 circles connected with narrow canal - when offset you get 2 polylines - arced segment + some buggy ending near canal passage), but in most cases it's somewhat doing what should (my polygon polylines for roofs - still buggy on ultimate test)...

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on July 01, 2018, 05:26:31 PM
Thank you very much for your attention to this code for so long. I haven't written the LISP program for a long time, and it's very unfamiliar .
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: chlh_jd on July 01, 2018, 06:17:03 PM
I think it's easy to solve the problem ,fowlling the pic. If I have time I'll try to solve it . Thank you !
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on July 08, 2018, 07:24:30 AM
I wonder is that you replied chlh_jd...
E-mail : chlh_jd@126.com
no one responded...
I am wondering are you OK...

P.S. Also ymg replied once after long time of no response and then nothing... Wondering is he OK...
If mail is bad then what is correct one?
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: pedroantonio on July 13, 2018, 03:39:18 AM
Hi   ribarm. I am  confused with all this changes. Can you post the final code ?  Is it possible to add a roof tiles hatch ?

Thanks
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on July 13, 2018, 06:50:21 AM
Hi   ribarm. I am  confused with all this changes. Can you post the final code ?  Is it possible to add a roof tiles hatch ?

Thanks

2droof-final.lsp is final version... What I showed is that its not perfect - in 90% cases it should solve ridge lines, still with some complex polylines as references it may fail... The whole thing about this topic is to make you think and code your own solution... As I am not the author of posted lisp and its complex for me to understand it, I can't help further more with this code - I just pointed that there is a space for improvement... As for my version - it is for 3d lines, it uses offset (vla-offset) function and it also fails with complex solutions... Beside all this it quite slow - for DWG I posted it took almost 2 hours and then you had to remove some additional lines to get final roof solution after which I reconstructed 3d solid...
I can't post my version as I struggled to make it doing as it is doing solutions like it is for now - some additional lines had to be removed and some lines are broken - you have to reconstruct single from 2 or more collinear lines...
So maybe some day if I find it beneficial for me I'll post it... The thing for you to watch if you go following my steps is to know that offset command can make some lacks when complex roof is wanted, so you have to account for that and result may be correct but with additional not needed 3d lines...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 23, 2018, 02:17:57 AM
Here are *.vlx files of my version... Still for some complex roofs you have to manually find solution and only then apply solution to 3DSOLID converter...
If someone is interested for lisp code though, you can make a small purchase - contact me at : ribarm@gmail.com
Still I am not sure that purchasing will work as I only have credit card viable for my country - Belgrade, Serbia, Europe...
We'll see, but here are *.vlx which are doing the same thing, only you can't modify them...

Regards, M.R.

[EDIT : I've changed a little main *.vlx (found some lacks)...]

[EDIT : I've changed my mind - who downloaded, its downloaded, now I am removing attachments... Only if I have response on my mail and concrete offer - maybe applications exchange or something else - I may post my lisps to him/her... Thanks for understanding...]

[EDIT : I thought ab this and I decided that I still want to be generous, so I'll attach again my *.vlx files... Since last time there have been changing in coding so this version is 3-4 times faster and it gives better results... Still it is slow in comparison to chlh_jd's code, but my version won't fail no matter how reference closed polygonal polyline looks like... Further more I did everything I could with current (vla-offset), so final return of execution of routine will always be with as much as possible lines that are relevant for solution (or solutions if there are more like roof-ultimate-test.dwg), so you always need to remove sufficient lines and leave only those that make solution you want... Afterwards when drawing is finished - solution, you can apply roof-mr-3dlines23dsolid.vlx converter in order to make 3d solid - of course for this CAD has to have implemented SURFSCULPT command... - there have also been slight change in this routine too - it's now little slower in converting, but it's better as far as my testings prove...]

[Previously attached files :
roof-mr-3dlines23dsolid.vlx
roof-mr-3dlines-convex+concave.vlx]

[*.vlx files removed for commercial reasons...]
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 13, 2018, 07:14:23 AM
Hi, it's me again...
I see that everyone is having CAD installed with wrong offset algorithm and so you also have it wrong chlh_jd... I can only say that it's totally unnecessary to struggle with bad algorithm and try to fix what's apparently wrong in the routine that is using it as reference (my code), so someone has to take that step in coding new offset... I see that, apparently, if offset would yield good results, than on every polygonal LWPOLYLINE as reference - EXTRUDE - Taper option would also give what's supposed to - no apex 3d solid mess if extruding distance larger than top apex of tapered shape with some angle specified... Autodesk would have no problems in implementing new algorithm in command EXTRUDE... What I am saying is that no matter I posted bad *.vlx files, codes that I have now are 100% correct I've checked them many many times... And no matter if on some cases it could draw more lines that actually should be, you could then (if offset good) easily remove them - this happens only on one case I tested and that's when ridge lines are collinear with each others... So there is no solution even now for this task, your code has some problems with interference of ridges and my code with offset (vla-offset) to be more specific... So to fix this once for all, there should be working made on source of problem (cause should be fixed in order to make cure to all consequences that may rise afterwards)... I know that maybe I am just imagining, but I hope someone that have the knowledge by creating original offset could read this post and perhaps step into process of making new better algorithm based on old implemented one... First step would be to make it correct for polyline open/closed and only then take action further and implement it on other curves (circles, ellipses, splines and so...)... I'll attach picture of what I am thinking ab ultimate test and your image chlh_jd...
I hope my remarks would be considered maybe in forthcoming future and I can only hope that I am not so optimistic that someone could think that I am saying rubbish right now...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 13, 2018, 08:11:05 AM
In attachment is dwg with I suppose correct offset...
M.R.

[EDIT : Attached additional dwg showing both solutions - this is what I gat on my slowest quad core PC after about half an hour...]
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 15, 2018, 08:24:27 AM
Post updated here :
http://www.theswamp.org/index.php?topic=41837.msg589751#msg589751

One more thing, I've edited a little chlh_jd's routine so that it is little more reliable and I've shorten code for it didn't need repeating of one paragraph of codes - I've made (defun) of that and I called it when it was appropriate IMHO... So I'll attach my revision too, but still it can't solve roof-ultimate-test.dwg... Maybe someone else with more understanding of what's going on could do it better, but I'll stick for now with my sometimes 100 times slower version - vlx files posted in above link...

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 17, 2018, 03:28:43 AM
Another one, maybe my 2droof-final.lsp is too bad...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 18, 2018, 08:42:57 AM
611 seconds for this one with my *.vlx...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 18, 2018, 02:13:01 PM
Actually I only now saw... Even my *.vlx couldn't solve this previously posted one... Red lines are manually reconstructed - they were missing...

[EDIT : I've corrected my *.vlx and reattached it again... It finished roof solution somewhat slower for 907 seconds, but it did the job...]
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 19, 2018, 01:26:02 PM
Another one...
And I don't want to post more... Just for the one that can fix errors I think it's easy to draw and check until it's fixed... There is my *.vlx if you haven't noticed if something is too complex to figure out...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 22, 2018, 08:32:10 AM
Since I don't like to keep things in my library that aren't working as supposed to, I've played a little again with 2droof-final.lsp... All I did is quick fix in that "separated" (defun errf ( errn errm ) ... ) that now has 2 arguments both iterating so that every combination of planes is checked... Still it doesn't work in some cases, but in my last DWG - 42 secs.dwg it did the job... As this is quick fix and it is only good if pline don't have too many vertices (planes), you can try 2droof-final-new.lsp... In all other cases where you have too many vertices, don't even think of this - it may finish almost never and still when it finishes the result may be unpredictable... So for this cases (too many vertices) there is mod of it called 2droof-final-quick.lsp... But all in all my vlx - slower than 2droof-final-quick.lsp and in some cases quicker than 2droof-final-new.lsp (too many vertices) yields always the best possible from set reference polygonal closed lwpolyline... So I don't know, I tried again, but this 2d routine is too complex and seems unpredictable no matter its quick (quick version)... Still I think that this my last revision is somewhat better than previously posted lsp... I'll attach them both, and you do what you think is the best...
M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 24, 2018, 06:09:42 AM
A little better formatted codes and some interventions... So latest revisions in this attachment...

[EDIT : For those that dowloaded - change (if (< loopn nnn) (progn ... )) to (if (<= loopn nnn) (progn ... ))
I'll reattach again...]

[EDIT : There was one more edit in the code... So now I think it's fine...]

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 25, 2018, 06:31:10 AM
2droof-final-new.lsp slightly changed... Final touch...

[EDIT : Now it's all OK...]

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: MSTG007 on September 25, 2018, 07:42:18 AM
Nice Job!
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 25, 2018, 07:48:36 AM
Nice Job!

Thanks...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on October 01, 2018, 09:05:00 AM
Variations on the same theme... Basically 2droof-final-new.lsp is now the same as 2droof-final-n-m-loop.lsp... I named new lisps by iteration priorities - so n is first which means that it iterates firstly, then m, and last loop... Based on those 3 variable that iterate over and over until process is finished there are 6 combinations... Still I leaved 2droof-final-new.lsp, but now it firstly looks for best loop and then process only that loop... There is also 2droof-final-new-looplst.lsp that looks for all best loops and then process them all - like shorter version of 2droof-final-n-m-loop.lsp... Also I made modifications to 2droof-final-quick.lsp - now it expects correct input combination and reports solution success percentage... All in all main code is unchanged - it still can't solve roof-ultimate-test.dwg... I tested loop=42 which was found by 2droof-final-new.lsp as best and success percentage was only 90/102 found closed ridges boundaries, so it's almost no need for calculations - loop=1, errn=0, errm=0 is 89/102 success... I think based on picture chlh_jd posted that he has something better as my output isn't the same as his, but I think that some new replies won't happen unless there is good will from him as an author...
So long for now from me - everything is in *.lsp form and hopefully you can make mods if you find some lacks in my interventions...
Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: pedroantonio on October 01, 2018, 10:08:21 AM
Hi ribarm. I think that your code for the roofs is perfect. No one will use it for something so complicated like your example.  :-D

Nice job
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on October 01, 2018, 10:40:58 AM
I had some mistake in 2droof-final-new.lsp and 2droof-final-new-looplst.lsp, so I reattached *.zip in my previous post... Actually roof-ultimate-test.dwg was tested on 42 loop and it did find best of that loop and that is errn=8 and errm=0... When you test it with 2droof-final-quick.lsp and input those parameters, solution success should be 99/102 with some lines crossing... So routines are good I think now - I just moved one paragraph just after (if (<= errm nnn) (progn ...) ... ) to the end of that progn... I think that with that quick fix it should output result correctly...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on October 01, 2018, 11:06:48 AM
No that quick fix was wrong... Reattached *.zip, I think that now it's all fine...

[EDIT : One more reattach... Sorry, lack of concentration...]
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: pedroantonio on October 01, 2018, 12:14:08 PM
You have 9 lisp routins. What lisp is the best to use ?
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on October 01, 2018, 01:02:57 PM
You have 9 lisp routins. What lisp is the best to use ?

If you don't have time, then this order :
1. 2droof-final-quick.lsp
2. 2droof-final-new.lsp
3. 2droof-final-new-looplst.lsp - I would still avoid 3. and 4. - it's time consuming...
4. 2droof-final-n-m-loop.lsp - you don't know correct combination of errn, errm, loop so you have to wait until you get it, or if not possible until you get best combination of elapsed time...
4-9. other lisps are just variations - you don't know combination so you practically can't figure out which one is the best to apply...

If reference polyline is too complex like roof-ultimate-test.dwg, after 1. and 2., the best is to use my roof-mr-3dlines-convex+concave.vlx posted earlier on page 11...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on October 02, 2018, 07:27:54 AM
Reattached *.zip, found again some mistakes...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ahsattarian on December 29, 2020, 01:58:06 PM
This is Modified of the lisps   :
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 03, 2021, 03:40:20 PM
Here is one version I wrote, but it's somewhat quirky... The main problem with this is that it's too slow, but I hope it's good for studying... We'll be grateful if someone could improve it...

Regards, all the best, M.R.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:2droof ( / unique mid clockwise-p LM:Inside-p processpla distp2t removesingles findipinterschilds processtxtipl process lw ti n pl tl tlo pla plaa ipl ipll ipldtl ipldtlo ip p1p2 tt t1 t2 a f lil txtipl tll d al iplo p pal fff ipp lipl dd ippp ipo )
  2.  
  3.   (defun unique ( l )
  4.     (if l (cons (car l) (unique (vl-remove-if '(lambda ( x ) (equal x (car l) 1e-6)) l))))
  5.   )
  6.  
  7.   (defun mid ( p1 p2 )
  8.     (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) p1 p2)
  9.   )
  10.  
  11.   (defun clockwise-p ( p1 p2 p3 )
  12.     (minusp (sin (- (angle p1 p3) (angle p1 p2))))
  13.   )
  14.  
  15.   (defun LM:Inside-p ( pt ent / unit v^v _GroupByNum fd1 fd2 par lst nrm obj tmp )
  16.  
  17.     (vl-load-com)
  18.  
  19.     (defun unit ( v / d )
  20.       (if (not (equal (setq d (distance '(0.0 0.0 0.0) v)) 0.0 1e-8))
  21.         (mapcar '(lambda ( x ) (/ x d)) v)
  22.       )
  23.     )
  24.  
  25.     (defun v^v ( u v )
  26.       (list
  27.         (- (* (cadr u) (caddr v)) (* (caddr u) (cadr v)))
  28.         (- (* (caddr u) (car v)) (* (car u) (caddr v)))
  29.         (- (* (car u) (cadr v)) (* (cadr u) (car v)))
  30.       )
  31.     )
  32.  
  33.     (defun _GroupByNum ( l n / r )
  34.       (if l
  35.         (cons (reverse (repeat n (setq r (cons (car l) r) l (cdr l)) r)) (_GroupByNum l n))
  36.       )
  37.     )
  38.  
  39.     (if (= (type ent) 'VLA-OBJECT)
  40.       (setq obj ent
  41.             ent (vlax-vla-object->ename ent))
  42.       (setq obj (vlax-ename->vla-object ent))
  43.     )
  44.  
  45.     (if (vlax-curve-isplanar ent)
  46.       (progn
  47.         (setq fd1 (vlax-curve-getfirstderiv ent (setq par (vlax-curve-getstartparam ent))))
  48.         (while (or (equal fd1 (setq fd2 (vlax-curve-getfirstderiv ent (setq par (+ par 0.001)))) 1e-3) (null (setq nrm (unit (v^v fd1 fd2))))))
  49.         (setq lst
  50.           (_GroupByNum
  51.             (vlax-invoke
  52.               (setq tmp
  53.                 (vlax-ename->vla-object
  54.                   (entmakex
  55.                     (list
  56.                       (cons 0 "RAY")
  57.                       (cons 100 "AcDbEntity")
  58.                       (cons 100 "AcDbRay")
  59.                       (cons 10 pt)
  60.                       (cons 11 (trans '(1. 0. 0.) nrm 0))
  61.                     )
  62.                   )
  63.                 )
  64.               )
  65.               'IntersectWith obj acextendnone
  66.             ) 3
  67.           )
  68.         )
  69.         (vla-delete tmp)
  70.         ;; gile:
  71.         (and
  72.           lst
  73.           (not (vlax-curve-getparamatpoint ent pt))
  74.           (= 1 (rem (length (vl-remove-if (function (lambda ( p / pa p- p+ p0 )
  75.                                                       (setq pa (vlax-curve-getparamatpoint ent p))
  76.                                                       (and (setq p- (cond ((setq p- (vlax-curve-getPointatParam ent (- pa 1e-8)))
  77.                                                                            (trans p- 0 nrm)
  78.                                                                           )
  79.                                                                           ((trans (vlax-curve-getPointatParam ent (- (vlax-curve-getEndParam ent) 1e-8)) 0 nrm)
  80.                                                                           )
  81.                                                                     )
  82.                                                            )
  83.                                                            (setq p+ (cond ((setq p+ (vlax-curve-getPointatParam ent (+ pa 1e-8)))
  84.                                                                            (trans p+ 0 nrm)
  85.                                                                           )
  86.                                                                           ((trans (vlax-curve-getPointatParam ent (+ (vlax-curve-getStartParam ent) 1e-8)) 0 nrm)
  87.                                                                           )
  88.                                                                     )
  89.                                                            )
  90.                                                            (setq p0 (trans pt 0 nrm))
  91.                                                            (<= 0 (* (sin (angle p0 p-)) (sin (angle p0 p+)))) ;; LM Mod
  92.                                                       )
  93.                                                     )
  94.                                           ) lst
  95.                             )
  96.                     ) 2
  97.                )
  98.           )
  99.         )
  100.       )
  101.     )
  102.   )
  103.  
  104.   (defun processpla ( pla / ipl pla1 pla2 )
  105.     (setq pla1 pla)
  106.     (foreach p1 pla1
  107.       (setq pla2 (vl-remove p1 pla1))
  108.       (foreach p2 pla2
  109.         (setq ipl (cons (inters (car p1) (polar (car p1) (cadr p1) 1.0) (car p2) (polar (car p2) (cadr p2) 1.0) nil) ipl))
  110.       )
  111.     )
  112.     (setq ipl (vl-remove-if-not '(lambda ( p ) (LM:Inside-p p lw)) (vl-remove nil ipl)))
  113.   )
  114.  
  115.   (defun distp2t ( p tt )
  116.     (distance p (inters p (polar p (+ (angle (car tt) (cadr tt)) (* 0.5 pi)) 1.0) (car tt) (cadr tt) nil))
  117.   )
  118.  
  119.   (defun removesingles ( l / a )
  120.     (while (setq a (vl-some '(lambda ( x ) (if (= (1- (length l)) (length (vl-remove-if '(lambda ( y ) (equal x y 1e-6)) l))) x)) l))
  121.       (setq l (vl-remove a l))
  122.     )
  123.     l
  124.   )
  125.  
  126.   (defun findipinterschilds ( ip pla / pla1 pla2 r )
  127.     (setq pla1 pla)
  128.     (foreach p1 pla1
  129.       (setq pla2 (vl-remove p1 pla1))
  130.       (foreach p2 pla2
  131.         (if (equal ip (inters (car p1) (polar (car p1) (cadr p1) 1e+99) (car p2) (polar (car p2) (cadr p2) 1e+99) t) 1e-6)
  132.           (setq r (list p1 p2))
  133.         )
  134.       )
  135.     )
  136.     r
  137.   )
  138.  
  139.   (defun processtxtipl ( tl / ipl tl1 tl2 )
  140.     (setq tl1 tl)
  141.     (foreach t1 tl1
  142.       (setq tl2 (vl-remove t1 tl1))
  143.       (foreach t2 tl2
  144.         (setq ipl (cons (inters (car t1) (polar (car t1) (angle (car t1) (cadr t1)) 1.0) (car t2) (polar (car t2) (angle (car t2) (cadr t2)) 1.0) nil) ipl))
  145.       )
  146.     )
  147.     (setq ipl (vl-remove nil ipl))
  148.   )
  149.  
  150.   (defun process nil
  151.     (setq d (car (cadr (car (vl-member-if '(lambda ( x ) (equal (car x) ip 1e-6)) ipldtl)))))
  152.     (if (null d)
  153.       (setq d (car (cadr (car (vl-member-if '(lambda ( x ) (equal (car x) ip 1e-6)) ipldtlo)))))
  154.     )
  155.     (if ip
  156.       (setq p1p2 (findipinterschilds ip pla))
  157.       (setq p1p2 nil)
  158.     )
  159.     (setq f nil)
  160.     (if p1p2
  161.       (progn
  162.         (setq tt (vl-some '(lambda ( x ) (if (and (assoc (car x) p1p2) (assoc (cadr x) p1p2)) (progn (setq f t) x))) tl))
  163.         (if (or (vl-some '(lambda ( x ) (equal (distance ip (car (car p1p2))) (+ (distance ip x) (distance x (car (car p1p2)))) 1e-6)) (vl-remove (car (car p1p2)) (vl-remove ip (mapcar 'car ipldtl)))) (vl-some '(lambda ( x ) (equal (distance ip (car (cadr p1p2))) (+ (distance ip x) (distance x (car (cadr p1p2)))) 1e-6)) (vl-remove (car (cadr p1p2)) (vl-remove ip (mapcar 'car ipldtl)))))
  164.           (setq f nil)
  165.         )
  166.       )
  167.     )
  168.   )
  169.  
  170.   (setq lw (car (entsel)))
  171.   (setq ti (car (_vl-times)))
  172.   (setq n (cdr (assoc 90 (entget lw))))
  173.   (setq pl (mapcar 'cdr (vl-remove-if '(lambda ( x ) (/= (car x) 10)) (entget lw))))
  174.   (setq tl (mapcar '(lambda ( a b ) (list a b)) pl (append (cdr pl) (list (car pl)))))
  175.   (setq tlo tl)
  176.   (setq pla (mapcar '(lambda ( p mp ) (list p (angle p mp))) pl (mapcar '(lambda ( a b ) (if (clockwise-p (car a) (car b) (cadr b)) (mid (polar (cadr a) (angle (cadr a) (car a)) -1.0) (polar (car b) (angle (car b) (cadr b)) -1.0)) (mid (polar (cadr a) (angle (cadr a) (car a)) 1.0) (polar (car b) (angle (car b) (cadr b)) 1.0)))) (cons (last tl) tl) tl)))
  177.   (setq plaa pla)
  178.   (while (> n 0)
  179.     (if (null ipll)
  180.       (setq ipl (processpla plaa))
  181.       (setq ipl (processpla pla))
  182.     )
  183.     (if (equal ipl iplo 1e-6)
  184.       (setq n 0)
  185.     )
  186.     (if fff
  187.       (progn
  188.         (setq pl (mapcar 'car pla))
  189.         (foreach x pl
  190.           (setq lil (cons (list x ip) lil))
  191.         )
  192.         (setq n 0)
  193.       )
  194.       (progn
  195.         (if (null ipll)
  196.           (progn
  197.             (setq ipldtlo (mapcar '(lambda ( p ) (list p (vl-sort (mapcar '(lambda ( tt ) (distp2t p tt)) tlo) '<))) ipl))
  198.             (setq ipldtlo (mapcar '(lambda ( x ) (list (car x) (removesingles (cadr x)))) ipldtlo))
  199.             (setq ipldtlo (vl-remove-if '(lambda ( x ) (< (- (length (cadr x)) (length (vl-remove-if '(lambda ( y ) (equal y (car (cadr x)) 1e-6)) (cadr x)))) 3)) ipldtlo))
  200.           )
  201.           (progn
  202.             (setq ipldtl (mapcar '(lambda ( p ) (list p (vl-sort (mapcar '(lambda ( tt ) (distp2t p tt)) tlo) '<))) ipl))
  203.             (setq ipldtl (mapcar '(lambda ( x ) (list (car x) (removesingles (cadr x)))) ipldtl))
  204.             (setq ipldtl (vl-remove-if '(lambda ( x ) (< (- (length (cadr x)) (length (vl-remove-if '(lambda ( y ) (equal y (car (cadr x)) 1e-6)) (cadr x)))) 3)) ipldtl))
  205.             (if ipll
  206.               (setq ipldtl (unique (vl-remove-if '(lambda ( x ) (vl-member-if '(lambda ( y ) (equal (car x) y 1e-6)) ipll)) ipldtl)))
  207.             )
  208.           )
  209.         )
  210.         (setq ipldtlo (vl-remove-if '(lambda ( x ) (vl-position (car x) ipll)) ipldtlo))
  211.         (setq ip (car (car (vl-sort ipldtlo '(lambda ( a b ) (< (car (cadr a)) (car (cadr b))))))))
  212.         (setq ipldtlo (vl-remove-if '(lambda ( x ) (equal (car x) ip 1e-6)) ipldtlo))
  213.         (setq d (car (cadr (car (vl-member-if '(lambda ( x ) (equal (car x) ip 1e-6)) ipldtlo)))))
  214.         (setq ipp nil dd nil ipo nil)
  215.         (if (progn (process) (not f))
  216.           (if ipll
  217.             (progn
  218.               (setq ipo ip)
  219.               (foreach y (reverse pal)
  220.                 (setq ipp (cons (vl-remove-if-not '(lambda ( x ) (vl-some '(lambda ( a ) (or (equal (rem (+ a pi) (* 2 pi)) (angle (car y) x) 1e-6) (equal a (angle (car y) x) 1e-6))) (cadr y))) (mapcar 'car (vl-sort ipldtl '(lambda ( a b ) (> (car (cadr a)) (car (cadr b))))))) ipp))
  221.               )
  222.               (setq dd 1e+99)
  223.               (mapcar '(lambda ( x y ) (if x (mapcar '(lambda ( xx ) (if (and (< (distance xx y) dd) (not (vl-some '(lambda ( a ) (vl-some '(lambda ( b ) (equal (distance xx a) (+ (distance xx b) (distance b a)) 1e-6)) (vl-remove xx (vl-remove a (append (mapcar 'car pal) (mapcar 'car ipldtl)))))) (vl-remove xx (append (mapcar 'car pal) (mapcar 'car ipldtl)))))) (setq dd (distance xx y) ip xx))) x))) ipp (mapcar 'car pal))
  224.             )
  225.           )
  226.         )
  227.         (if (equal ip ipo 1e-6)
  228.           (progn
  229.             (setq dd 1e+99)
  230.             (mapcar '(lambda ( x y ) (if x (mapcar '(lambda ( xx ) (if (< (distance xx y) dd) (setq dd (distance xx y) ip xx))) x))) ipp (mapcar 'car pal))
  231.           )
  232.         )
  233.         (if (vl-every '(lambda ( x ) (equal ip (inters (car (car pla)) (polar (car (car pla)) (cadr (car pla)) 1.0) (car x) (polar (car x) (cadr x) 1.0) nil) 1e-6)) (cdr pla))
  234.           (setq fff t)
  235.         )
  236.         (setq al nil a nil)
  237.         (process)
  238.         (if (and tt f)
  239.           (progn
  240.             (setq tl (vl-remove tt tl))
  241.             (setq n (1- n))
  242.           )
  243.         )
  244.         (if f
  245.           (progn
  246.             (setq t1 (vl-some '(lambda ( x ) (if (or (assoc (car x) (list (car p1p2))) (assoc (cadr x) (list (car p1p2)))) x)) tl))
  247.             (setq t2 (vl-some '(lambda ( x ) (if (or (assoc (car x) (list (cadr p1p2))) (assoc (cadr x) (list (cadr p1p2)))) x)) tl))
  248.           )
  249.           (setq t1 nil t2 nil)
  250.         )
  251.         (if ip
  252.           (setq ipll (cons ip ipll))
  253.         )
  254.         (if (and t1 t2)
  255.           (progn
  256.             (setq a (angle (inters (car t1) (cadr t1) (car t2) (cadr t2) nil) ip))
  257.             (if a
  258.               (progn
  259.                 (setq pla (cons (list ip a) pla))
  260.                 (setq pla (vl-remove (car p1p2) pla) pla (vl-remove (cadr p1p2) pla))
  261.                 (setq lil (cons (list (car (car p1p2)) ip) lil))
  262.                 (setq lil (cons (list (car (cadr p1p2)) ip) lil))
  263.                 (setq pal (cons (list ip (list a)) pal))
  264.               )
  265.             )
  266.           )
  267.           (if (and ip (null fff))
  268.             (progn
  269.               (setq lil (cons (list (car (car p1p2)) ip) lil))
  270.               (setq lil (cons (list (car (cadr p1p2)) ip) lil))
  271.               (if (setq p (vl-some '(lambda ( x ) (if (vl-position x plaa) x)) p1p2))
  272.                 (progn
  273.                   (setq n (1- n))
  274.                   (setq tll (vl-remove-if-not '(lambda ( x ) (vl-some '(lambda ( y ) (vl-member-if '(lambda ( z ) (equal z p 1e-6)) y)) x)) tlo))
  275.                   (setq tll (car (vl-remove-if-not '(lambda ( x ) (and (vl-member-if '(lambda ( y ) (equal y (car x) 1e-6)) (vl-remove nil (mapcar 'car lil))) (vl-member-if '(lambda ( y ) (equal y (cadr x) 1e-6)) (vl-remove nil (mapcar 'car lil))))) tll)))
  276.                   (setq tl (vl-remove tll tl))
  277.                 )
  278.               )
  279.               (setq tll (vl-remove-if-not '(lambda ( x ) (equal d (distp2t ip x) 1e-6)) tl))
  280.               (setq txtipl (processtxtipl tll))
  281.               (setq txtipl (vl-remove-if '(lambda ( x ) (vl-member-if '(lambda ( y ) (equal x y 1e-6)) pl)) txtipl))
  282.               (setq al (mapcar '(lambda ( p ) (angle p ip)) txtipl))
  283.               (setq al (vl-remove-if '(lambda ( x ) (vl-member-if '(lambda ( y ) (equal x (cadr y) 1e-6)) p1p2)) al))
  284.               (setq al (unique al))
  285.               (if al
  286.                 (progn
  287.                   (setq pla (append pla (mapcar '(lambda ( a ) (list ip a)) al)))
  288.                   (setq pal (cons (list ip al) pal))
  289.                 )
  290.               )
  291.               (setq pla (vl-remove (car p1p2) pla) pla (vl-remove (cadr p1p2) pla))
  292.             )
  293.           )
  294.         )
  295.       )
  296.     )
  297.     (setq iplo ipl)
  298.   )
  299.   (setq lil (vl-remove-if '(lambda ( x ) (equal (car x) (cadr x) 1e-6)) lil))
  300.   (setq lil (unique lil))
  301.   (setq lil (vl-remove-if '(lambda ( x ) (vl-some '(lambda ( y ) (null y)) x)) lil))
  302.   (setq lipl (apply 'append lil))
  303.   (setq lipl (unique (vl-remove-if '(lambda ( x ) (or (= (length (vl-remove x lipl)) (1- (length lipl))) (= (length (vl-remove x lipl)) (- (length lipl) 3)))) lipl)))
  304.   (if lipl
  305.     (setq lil (cons (list (car lipl) (cadr lipl)) lil))
  306.   )
  307.   (foreach li lil
  308.     (entmake (list '(0 . "LINE") (cons 10 (car li)) (cons 11 (cadr li))))
  309.   )
  310.   (prompt "\nElapsed time : ") (princ (rtos (- (car (_vl-times)) ti) 2 20)) (prompt " milliseconds...")
  311.   (princ)
  312. )
  313.  
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 05, 2021, 12:46:02 PM
I've revised the code a little, but I see that no one is interested... It can't operate with orthogonal shapes, which are typical roof solutions, but it can do simple random polygons - concave+convex... It is horribly slow and it is incorrect with complex shapes like roof-ultimate.dwg I posted... I tested it and with that, but it took almost 4 hours on my slow PC and solution is not even close to existing routine by ch_lhjd... So don't test it until you're 100% sure it's fine...
In my observations, the main problem is finding "ip" point - here is tricky part of the code :

Code: [Select]
...
        (setq ipp nil dd nil ipo nil)
        (if (progn (process) (not f))
          (if ipll
            (progn
              (setq ipo ip)
              (foreach y (reverse pal)
                (setq ipp (cons (vl-remove-if-not '(lambda ( x ) (vl-some '(lambda ( a ) (or (equal (rem (+ a pi) (* 2 pi)) (angle (car y) x) 1e-6) (equal a (angle (car y) x) 1e-6))) (cadr y))) (mapcar 'car (vl-sort ipldtl '(lambda ( a b ) (> (car (cadr a)) (car (cadr b))))))) ipp))
              )
              (setq dd 1e+99)
              (mapcar '(lambda ( x y ) (if x (mapcar '(lambda ( xx ) (if (and (< (distance xx y) dd) (not (vl-some '(lambda ( a ) (vl-some '(lambda ( b ) (equal (distance xx a) (+ (distance xx b) (distance b a)) 1e-6)) (vl-remove xx (vl-remove a (append (mapcar 'car pal) (mapcar 'car ipldtl)))))) (vl-remove xx (append (mapcar 'car pal) (mapcar 'car ipldtl)))))) (setq dd (distance xx y) ip xx))) x))) ipp (mapcar 'car pal))
            )
          )
        )
        ;;;;; This portion that follows is crucial, but I can't discover how to get correct ip from list ipp - it may be that some of points from ipp are good and some are bad... ;;;;;;
        (if (equal ip ipo 1e-6)
          (progn
            (setq dd 1e+99)
            (mapcar '(lambda ( x y ) (if x (mapcar '(lambda ( xx ) (if (< (distance xx y) dd) (setq dd (distance xx y) ip xx))) x))) ipp (mapcar 'car pal))
          )
        )
...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 05, 2021, 05:07:15 PM
I've changed a code once again just a little, and perhaps this is better, but I doubt...

Code: [Select]
...
        (if (equal ip ipo 1e-6)
          (progn
            (setq ipp (apply 'append ipp))
            (setq ipp (mapcar '(lambda ( p ) (list p (findipinterschilds p pla))) ipp))
            (setq ip (car (car (vl-sort ipp '(lambda ( a b ) (< (+ (distance (car a) (car (mapcar 'car (cadr a)))) (distance (car a) (cadr (mapcar 'car (cadr a))))) (+ (distance (car b) (car (mapcar 'car (cadr b)))) (distance (car b) (cadr (mapcar 'car (cadr b)))))))))))
          )
        )
...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 06, 2021, 12:00:33 AM
Here is how my ultimate-test looks like after almost 5 hours of calculations...
So advice, don't test it on complex samples until algorithm is fully correct...

M.R.

(https://serving.photos.photobox.com/11489974f262bdb143e765d21b4d08057b66dd76a2f98753ec218f78a948a2e94108aa8c.jpg)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 06, 2021, 04:19:01 AM
I've optimized my version and now it's faster, but not very... At least I did what was possible and I could do...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:2droof ( / unique mid clockwise-p LM:Inside-p processpla distp2t removesingles findipinterschilds processtxtipl process _vl-position car-sort car-vl-member-if lw ti n pl tl tlo pla plaa ipl ipll ipldtl ipldtlo ip p1p2 tt t1 t2 a f lil txtipl tll d al iplo p pal fff ipp lipl dd ippp ipo )
  2.  
  3.   (defun unique ( l )
  4.     (if l (cons (car l) (unique (vl-remove-if (function (lambda ( x ) (equal x (car l) 1e-6))) l))))
  5.   )
  6.  
  7.   (defun mid ( p1 p2 )
  8.     (mapcar (function (lambda ( a b ) (/ (+ a b) 2.0))) p1 p2)
  9.   )
  10.  
  11.   (defun clockwise-p ( p1 p2 p3 )
  12.     (minusp (sin (- (angle p1 p3) (angle p1 p2))))
  13.   )
  14.  
  15.   (defun LM:Inside-p ( pt ent / unit v^v _GroupByNum fd1 fd2 par lst nrm obj tmp )
  16.  
  17.     (vl-load-com)
  18.  
  19.     (defun unit ( v / d )
  20.       (if (not (equal (setq d (distance '(0.0 0.0 0.0) v)) 0.0 1e-8))
  21.         (mapcar (function (lambda ( x ) (/ x d))) v)
  22.       )
  23.     )
  24.  
  25.     (defun v^v ( u v )
  26.       (list
  27.         (- (* (cadr u) (caddr v)) (* (caddr u) (cadr v)))
  28.         (- (* (caddr u) (car v)) (* (car u) (caddr v)))
  29.         (- (* (car u) (cadr v)) (* (cadr u) (car v)))
  30.       )
  31.     )
  32.  
  33.     (defun _GroupByNum ( l n / r )
  34.       (if l
  35.         (cons (reverse (repeat n (setq r (cons (car l) r) l (cdr l)) r)) (_GroupByNum l n))
  36.       )
  37.     )
  38.  
  39.     (if (= (type ent) 'VLA-OBJECT)
  40.       (setq obj ent
  41.             ent (vlax-vla-object->ename ent))
  42.       (setq obj (vlax-ename->vla-object ent))
  43.     )
  44.  
  45.     (if (vlax-curve-isplanar ent)
  46.       (progn
  47.         (setq fd1 (vlax-curve-getfirstderiv ent (setq par (vlax-curve-getstartparam ent))))
  48.         (while (or (equal fd1 (setq fd2 (vlax-curve-getfirstderiv ent (setq par (+ par 0.001)))) 1e-3) (null (setq nrm (unit (v^v fd1 fd2))))))
  49.         (setq lst
  50.           (_GroupByNum
  51.             (vlax-invoke
  52.               (setq tmp
  53.                 (vlax-ename->vla-object
  54.                   (entmakex
  55.                     (list
  56.                       (cons 0 "RAY")
  57.                       (cons 100 "AcDbEntity")
  58.                       (cons 100 "AcDbRay")
  59.                       (cons 10 pt)
  60.                       (cons 11 (trans '(1. 0. 0.) nrm 0))
  61.                     )
  62.                   )
  63.                 )
  64.               )
  65.               'IntersectWith obj acextendnone
  66.             ) 3
  67.           )
  68.         )
  69.         (vla-delete tmp)
  70.         ;; gile:
  71.         (and
  72.           lst
  73.           (not (vlax-curve-getparamatpoint ent pt))
  74.           (= 1 (rem (length (vl-remove-if (function (lambda ( p / pa p- p+ p0 )
  75.                                                       (setq pa (vlax-curve-getparamatpoint ent p))
  76.                                                       (and (setq p- (cond ((setq p- (vlax-curve-getPointatParam ent (- pa 1e-8)))
  77.                                                                            (trans p- 0 nrm)
  78.                                                                           )
  79.                                                                           ((trans (vlax-curve-getPointatParam ent (- (vlax-curve-getEndParam ent) 1e-8)) 0 nrm)
  80.                                                                           )
  81.                                                                     )
  82.                                                            )
  83.                                                            (setq p+ (cond ((setq p+ (vlax-curve-getPointatParam ent (+ pa 1e-8)))
  84.                                                                            (trans p+ 0 nrm)
  85.                                                                           )
  86.                                                                           ((trans (vlax-curve-getPointatParam ent (+ (vlax-curve-getStartParam ent) 1e-8)) 0 nrm)
  87.                                                                           )
  88.                                                                     )
  89.                                                            )
  90.                                                            (setq p0 (trans pt 0 nrm))
  91.                                                            (<= 0 (* (sin (angle p0 p-)) (sin (angle p0 p+)))) ;; LM Mod
  92.                                                       )
  93.                                                     )
  94.                                           ) lst
  95.                             )
  96.                     ) 2
  97.                )
  98.           )
  99.         )
  100.       )
  101.     )
  102.   )
  103.  
  104.   (defun processpla ( pla / ipl pla1 pla2 )
  105.     (setq pla1 pla)
  106.     (foreach p1 pla1
  107.       (setq pla2 (vl-remove p1 pla1))
  108.       (foreach p2 pla2
  109.         (setq ipl (cons (inters (car p1) (polar (car p1) (cadr p1) 1.0) (car p2) (polar (car p2) (cadr p2) 1.0) nil) ipl))
  110.       )
  111.     )
  112.     (setq ipl (vl-remove-if-not (function (lambda ( p ) (LM:Inside-p p lw))) (vl-remove nil ipl)))
  113.   )
  114.  
  115.   (defun distp2t ( p tt )
  116.     (distance p (inters p (polar p (+ (angle (car tt) (cadr tt)) (* 0.5 pi)) 1.0) (car tt) (cadr tt) nil))
  117.   )
  118.  
  119.   (defun removesingles ( l / a )
  120.     (while (setq a (vl-some (function (lambda ( x ) (if (= (1- (length l)) (length (vl-remove-if (function (lambda ( y ) (equal x y 1e-6))) l))) x))) l))
  121.       (setq l (vl-remove a l))
  122.     )
  123.     l
  124.   )
  125.  
  126.   (defun findipinterschilds ( ip pla / pla1 pla2 r )
  127.     (setq pla1 pla)
  128.     (foreach p1 pla1
  129.       (setq pla2 (vl-remove p1 pla1))
  130.       (foreach p2 pla2
  131.         (if (equal ip (inters (car p1) (polar (car p1) (cadr p1) 1e+99) (car p2) (polar (car p2) (cadr p2) 1e+99) t) 1e-6)
  132.           (setq r (list p1 p2))
  133.         )
  134.       )
  135.     )
  136.     r
  137.   )
  138.  
  139.   (defun processtxtipl ( tl / ipl tl1 tl2 )
  140.     (setq tl1 tl)
  141.     (foreach t1 tl1
  142.       (setq tl2 (vl-remove t1 tl1))
  143.       (foreach t2 tl2
  144.         (setq ipl (cons (inters (car t1) (polar (car t1) (angle (car t1) (cadr t1)) 1.0) (car t2) (polar (car t2) (angle (car t2) (cadr t2)) 1.0) nil) ipl))
  145.       )
  146.     )
  147.     (setq ipl (vl-remove nil ipl))
  148.   )
  149.  
  150.   (defun process nil
  151.     (setq d (car (cadr (car-vl-member-if (function (lambda ( x ) (equal (car x) ip 1e-6))) ipldtl))))
  152.     (if (null d)
  153.       (setq d (car (cadr (car-vl-member-if (function (lambda ( x ) (equal (car x) ip 1e-6))) ipldtlo))))
  154.     )
  155.     (if ip
  156.       (setq p1p2 (findipinterschilds ip pla))
  157.       (setq p1p2 nil)
  158.     )
  159.     (setq f nil)
  160.     (if p1p2
  161.       (progn
  162.         (setq tt (vl-some (function (lambda ( x ) (if (and (assoc (car x) p1p2) (assoc (cadr x) p1p2)) (progn (setq f t) x)))) tl))
  163.         (if (or (vl-some (function (lambda ( x ) (equal (distance ip (car (car p1p2))) (+ (distance ip x) (distance x (car (car p1p2)))) 1e-6))) (vl-remove-if (function (lambda ( x ) (equal x (car (car p1p2)) 1e-6))) (vl-remove-if (function (lambda ( x ) (equal x ip 1e-6))) (mapcar (function car) ipldtl)))) (vl-some (function (lambda ( x ) (equal (distance ip (car (cadr p1p2))) (+ (distance ip x) (distance x (car (cadr p1p2)))) 1e-6))) (vl-remove-if (function (lambda ( x ) (equal x (car (cadr p1p2)) 1e-6))) (vl-remove-if (function (lambda ( x ) (equal x ip 1e-6))) (mapcar (function car) ipldtl)))))
  164.           (setq f nil)
  165.         )
  166.       )
  167.     )
  168.   )
  169.  
  170.   (defun _vl-position ( e l tol / car-vl-member-if )
  171.     (defun car-vl-member-if ( f l / ff r )
  172.       (setq ff (function (lambda ( x ) (if (apply f (list x)) (setq r x)))))
  173.       (vl-some ff l)
  174.       r
  175.     )
  176.     (vl-position (car-vl-member-if (function (lambda ( x ) (equal e x tol))) l) l)
  177.   )
  178.  
  179.   (defun car-sort ( lst cmp / rtn )
  180.     (setq rtn (car lst))
  181.     (foreach itm (cdr lst)
  182.       (if (apply cmp (list itm rtn))
  183.         (setq rtn itm)
  184.       )
  185.     )
  186.     rtn
  187.   )
  188.  
  189.   (defun car-vl-member-if ( f l / ff r )
  190.     (setq ff (function (lambda ( x ) (if (apply f (list x)) (setq r x)))))
  191.     (vl-some ff l)
  192.     r
  193.   )
  194.  
  195.  
  196. ;;; main 2droof ;;;
  197.  
  198.   (setq lw (car (entsel "\nPick a closed polygon : ")))
  199.   (setq ti (car (_vl-times)))
  200.   (setq n (cdr (assoc 90 (entget lw))))
  201.   (setq pl (mapcar (function cdr) (vl-remove-if (function (lambda ( x ) (/= (car x) 10))) (entget lw))))
  202.   (setq tl (mapcar (function (lambda ( a b ) (list a b))) pl (append (cdr pl) (list (car pl)))))
  203.   (setq tlo tl)
  204.   (setq pla (mapcar (function (lambda ( p mp ) (list p (angle p mp)))) pl (mapcar (function (lambda ( a b ) (if (clockwise-p (car a) (car b) (cadr b)) (mid (polar (cadr a) (angle (cadr a) (car a)) -1.0) (polar (car b) (angle (car b) (cadr b)) -1.0)) (mid (polar (cadr a) (angle (cadr a) (car a)) 1.0) (polar (car b) (angle (car b) (cadr b)) 1.0))))) (cons (last tl) tl) tl)))
  205.   (setq plaa pla)
  206.   (while (> n 0)
  207.     (if (null ipll)
  208.       (setq ipl (processpla plaa))
  209.       (setq ipl (processpla pla))
  210.     )
  211.     (if (equal ipl iplo 1e-6)
  212.       (setq n 0)
  213.     )
  214.     (if fff
  215.       (progn
  216.         (setq pl (mapcar (function car) pla))
  217.         (foreach x pl
  218.           (setq lil (cons (list x ip) lil))
  219.         )
  220.         (setq n 0)
  221.       )
  222.       (progn
  223.         (if (null ipll)
  224.           (progn
  225.             (setq ipldtlo (mapcar (function (lambda ( p ) (list p (vl-sort (mapcar (function (lambda ( tt ) (distp2t p tt))) tlo) (function <))))) ipl))
  226.             (setq ipldtlo (mapcar (function (lambda ( x ) (list (car x) (removesingles (cadr x))))) ipldtlo))
  227.             (setq ipldtlo (vl-remove-if (function (lambda ( x ) (< (- (length (cadr x)) (length (vl-remove-if (function (lambda ( y ) (equal y (car (cadr x)) 1e-6))) (cadr x)))) 3))) ipldtlo))
  228.           )
  229.           (progn
  230.             (setq ipldtl (mapcar (function (lambda ( p ) (list p (vl-sort (mapcar (function (lambda ( tt ) (distp2t p tt))) tlo) (function <))))) ipl))
  231.             (setq ipldtl (mapcar (function (lambda ( x ) (list (car x) (removesingles (cadr x))))) ipldtl))
  232.             (setq ipldtl (vl-remove-if (function (lambda ( x ) (< (- (length (cadr x)) (length (vl-remove-if (function (lambda ( y ) (equal y (car (cadr x)) 1e-6))) (cadr x)))) 3))) ipldtl))
  233.             (if ipll
  234.               (setq ipldtl (unique (vl-remove-if (function (lambda ( x ) (_vl-position (car x) ipll 1e-6))) ipldtl)))
  235.             )
  236.           )
  237.         )
  238.         (setq ipldtlo (vl-remove-if (function (lambda ( x ) (vl-position (car x) ipll))) ipldtlo))
  239.         (setq ip (car (car-sort ipldtlo (function (lambda ( a b ) (< (car (cadr a)) (car (cadr b))))))))
  240.         (setq ipldtlo (vl-remove-if (function (lambda ( x ) (equal (car x) ip 1e-6))) ipldtlo))
  241.         (setq d (car (cadr (car-vl-member-if (function (lambda ( x ) (equal (car x) ip 1e-6))) ipldtlo))))
  242.         (setq ipp nil dd nil ipo nil)
  243.         (if (progn (process) (not f))
  244.           (if ipll
  245.             (progn
  246.               (setq ipo ip)
  247.               (foreach y (reverse pal)
  248.                 (setq ipp (cons (vl-remove-if-not (function (lambda ( x ) (vl-some (function (lambda ( a ) (or (equal (rem (+ a pi) (* 2 pi)) (angle (car y) x) 1e-6) (equal a (angle (car y) x) 1e-6)))) (cadr y)))) (mapcar (function car) (vl-sort ipldtl (function (lambda ( a b ) (> (car (cadr a)) (car (cadr b)))))))) ipp))
  249.               )
  250.               (setq dd 1e+99)
  251.               (mapcar (function (lambda ( x y ) (if x (mapcar (function (lambda ( xx ) (if (and (< (distance xx y) dd) (not (vl-some (function (lambda ( a ) (vl-some (function (lambda ( b ) (equal (distance xx a) (+ (distance xx b) (distance b a)) 1e-6))) (vl-remove xx (vl-remove a (append (mapcar (function car) pal) (mapcar (function car) ipldtl))))))) (vl-remove xx (append (mapcar (function car) pal) (mapcar (function car) ipldtl)))))) (setq dd (distance xx y) ip xx)))) x)))) ipp (mapcar (function car) pal))
  252.             )
  253.           )
  254.         )
  255.         (if (equal ip ipo 1e-6)
  256.           (progn
  257.             (setq ipp (apply (function append) ipp))
  258.             (setq ipp (mapcar (function (lambda ( p ) (list p (findipinterschilds p pla)))) ipp))
  259.             (setq ip (car (car-sort ipp (function (lambda ( a b ) (< (+ (distance (car a) (car (mapcar (function car) (cadr a)))) (distance (car a) (cadr (mapcar (function car) (cadr a))))) (+ (distance (car b) (car (mapcar (function car) (cadr b)))) (distance (car b) (cadr (mapcar (function car) (cadr b)))))))))))
  260.           )
  261.         )
  262.         (if (vl-every (function (lambda ( x ) (equal ip (inters (car (car pla)) (polar (car (car pla)) (cadr (car pla)) 1.0) (car x) (polar (car x) (cadr x) 1.0) nil) 1e-6))) (cdr pla))
  263.           (setq fff t)
  264.         )
  265.         (setq al nil a nil)
  266.         (process)
  267.         (if (null p1p2)
  268.           (setq ip nil)
  269.         )
  270.         (if (and tt f)
  271.           (progn
  272.             (setq tl (vl-remove tt tl))
  273.             (setq n (1- n))
  274.           )
  275.         )
  276.         (if f
  277.           (progn
  278.             (setq t1 (vl-some (function (lambda ( x ) (if (or (assoc (car x) (list (car p1p2))) (assoc (cadr x) (list (car p1p2)))) x))) tl))
  279.             (setq t2 (vl-some (function (lambda ( x ) (if (or (assoc (car x) (list (cadr p1p2))) (assoc (cadr x) (list (cadr p1p2)))) x))) tl))
  280.           )
  281.           (setq t1 nil t2 nil)
  282.         )
  283.         (if ip
  284.           (setq ipll (cons ip ipll))
  285.         )
  286.         (if (and t1 t2 ip)
  287.           (progn
  288.             (setq a (angle (inters (car t1) (cadr t1) (car t2) (cadr t2) nil) ip))
  289.             (if a
  290.               (progn
  291.                 (setq pla (cons (list ip a) pla))
  292.                 (setq pla (vl-remove (car p1p2) pla) pla (vl-remove (cadr p1p2) pla))
  293.                 (setq lil (cons (list (car (car p1p2)) ip) lil))
  294.                 (setq lil (cons (list (car (cadr p1p2)) ip) lil))
  295.                 (setq pal (cons (list ip (list a)) pal))
  296.               )
  297.             )
  298.           )
  299.           (if (and ip (null fff))
  300.             (progn
  301.               (setq lil (cons (list (car (car p1p2)) ip) lil))
  302.               (setq lil (cons (list (car (cadr p1p2)) ip) lil))
  303.               (if (setq p (vl-some (function (lambda ( x ) (if (vl-position x plaa) x))) p1p2))
  304.                 (progn
  305.                   (setq n (1- n))
  306.                   (setq tll (vl-remove-if-not (function (lambda ( x ) (vl-some (function (lambda ( y ) (_vl-position p y 1e-6))) x))) tlo))
  307.                   (setq tll (car (vl-remove-if-not (function (lambda ( x ) (and (_vl-position (car x) (vl-remove nil (mapcar (function car) lil)) 1e-6) (_vl-position (cadr x) (vl-remove nil (mapcar (function car) lil)) 1e-6)))) tll)))
  308.                   (setq tl (vl-remove tll tl))
  309.                 )
  310.               )
  311.               (setq tll (vl-remove-if-not (function (lambda ( x ) (equal d (distp2t ip x) 1e-6))) tl))
  312.               (setq txtipl (processtxtipl tll))
  313.               (setq txtipl (vl-remove-if (function (lambda ( x ) (_vl-position x pl 1e-6))) txtipl))
  314.               (setq al (mapcar (function (lambda ( p ) (angle p ip))) txtipl))
  315.               (setq al (vl-remove-if (function (lambda ( x ) (_vl-position x (mapcar (function cadr) p1p2) 1e-6))) al))
  316.               (setq al (unique al))
  317.               (if al
  318.                 (progn
  319.                   (setq pla (append pla (mapcar (function (lambda ( a ) (list ip a))) al)))
  320.                   (setq pal (cons (list ip al) pal))
  321.                 )
  322.               )
  323.               (setq pla (vl-remove (car p1p2) pla) pla (vl-remove (cadr p1p2) pla))
  324.             )
  325.           )
  326.         )
  327.       )
  328.     )
  329.     (setq iplo ipl)
  330.   )
  331.   (setq lil (vl-remove-if (function (lambda ( x ) (equal (car x) (cadr x) 1e-6))) lil))
  332.   (setq lil (unique lil))
  333.   (setq lil (vl-remove-if (function (lambda ( x ) (vl-some (function (lambda ( y ) (null y))) x))) lil))
  334.   (setq lipl (apply (function append) lil))
  335.   (setq lipl (unique (vl-remove-if (function (lambda ( x ) (or (= (length (vl-remove x lipl)) (1- (length lipl))) (= (length (vl-remove x lipl)) (- (length lipl) 3))))) lipl)))
  336.   (if lipl
  337.     (setq lil (cons (list (car lipl) (cadr lipl)) lil))
  338.   )
  339.   (foreach li lil
  340.     (entmake (list '(0 . "LINE") (cons 10 (car li)) (cons 11 (cadr li))))
  341.   )
  342.   (prompt "\nElapsed time : ") (princ (rtos (- (car (_vl-times)) ti) 2 20)) (prompt " milliseconds...")
  343.   (princ)
  344. )
  345.  
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: MSTG007 on January 06, 2021, 07:36:32 AM
Crazy awesome, Great job!
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 07, 2021, 12:38:06 PM
I did it... This my version finds solutions sometimes different than built-in ACAD offset command preview... Take look at picture and DWG...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:2droof ( / unique mid clockwise-p LM:Inside-p processpla distp2t removesingles findipinterschilds processtxtipl process _vl-position car-sort car-vl-member-if lw ti n pl tl tlo pla plaa ipl ipll ipldtl ipldtlo ip p1p2 tt t1 t2 a f lil txtipl tll d al iplo p pal fff ipp lipl ippp dl itt )
  2.  
  3.   (defun unique ( l )
  4.     (if l (cons (car l) (unique (vl-remove-if (function (lambda ( x ) (equal x (car l) 1e-6))) l))))
  5.   )
  6.  
  7.   (defun mid ( p1 p2 )
  8.     (mapcar (function (lambda ( a b ) (/ (+ a b) 2.0))) p1 p2)
  9.   )
  10.  
  11.   (defun clockwise-p ( p1 p2 p3 )
  12.     (minusp (sin (- (angle p1 p3) (angle p1 p2))))
  13.   )
  14.  
  15.   (defun LM:Inside-p ( pt ent / unit v^v _GroupByNum fd1 fd2 par lst nrm obj tmp )
  16.  
  17.     (vl-load-com)
  18.  
  19.     (defun unit ( v / d )
  20.       (if (not (equal (setq d (distance '(0.0 0.0 0.0) v)) 0.0 1e-8))
  21.         (mapcar (function (lambda ( x ) (/ x d))) v)
  22.       )
  23.     )
  24.  
  25.     (defun v^v ( u v )
  26.       (list
  27.         (- (* (cadr u) (caddr v)) (* (caddr u) (cadr v)))
  28.         (- (* (caddr u) (car v)) (* (car u) (caddr v)))
  29.         (- (* (car u) (cadr v)) (* (cadr u) (car v)))
  30.       )
  31.     )
  32.  
  33.     (defun _GroupByNum ( l n / r )
  34.       (if l
  35.         (cons (reverse (repeat n (setq r (cons (car l) r) l (cdr l)) r)) (_GroupByNum l n))
  36.       )
  37.     )
  38.  
  39.     (if (= (type ent) 'VLA-OBJECT)
  40.       (setq obj ent
  41.             ent (vlax-vla-object->ename ent))
  42.       (setq obj (vlax-ename->vla-object ent))
  43.     )
  44.  
  45.     (if (vlax-curve-isplanar ent)
  46.       (progn
  47.         (setq fd1 (vlax-curve-getfirstderiv ent (setq par (vlax-curve-getstartparam ent))))
  48.         (while (or (equal fd1 (setq fd2 (vlax-curve-getfirstderiv ent (setq par (+ par 0.001)))) 1e-3) (null (setq nrm (unit (v^v fd1 fd2))))))
  49.         (setq lst
  50.           (_GroupByNum
  51.             (vlax-invoke
  52.               (setq tmp
  53.                 (vlax-ename->vla-object
  54.                   (entmakex
  55.                     (list
  56.                       (cons 0 "RAY")
  57.                       (cons 100 "AcDbEntity")
  58.                       (cons 100 "AcDbRay")
  59.                       (cons 10 pt)
  60.                       (cons 11 (trans '(1. 0. 0.) nrm 0))
  61.                     )
  62.                   )
  63.                 )
  64.               )
  65.               'IntersectWith obj acextendnone
  66.             ) 3
  67.           )
  68.         )
  69.         (vla-delete tmp)
  70.         ;; gile:
  71.         (and
  72.           lst
  73.           (not (vlax-curve-getparamatpoint ent pt))
  74.           (= 1 (rem (length (vl-remove-if (function (lambda ( p / pa p- p+ p0 )
  75.                                                       (setq pa (vlax-curve-getparamatpoint ent p))
  76.                                                       (and (setq p- (cond ((setq p- (vlax-curve-getPointatParam ent (- pa 1e-8)))
  77.                                                                            (trans p- 0 nrm)
  78.                                                                           )
  79.                                                                           ((trans (vlax-curve-getPointatParam ent (- (vlax-curve-getEndParam ent) 1e-8)) 0 nrm)
  80.                                                                           )
  81.                                                                     )
  82.                                                            )
  83.                                                            (setq p+ (cond ((setq p+ (vlax-curve-getPointatParam ent (+ pa 1e-8)))
  84.                                                                            (trans p+ 0 nrm)
  85.                                                                           )
  86.                                                                           ((trans (vlax-curve-getPointatParam ent (+ (vlax-curve-getStartParam ent) 1e-8)) 0 nrm)
  87.                                                                           )
  88.                                                                     )
  89.                                                            )
  90.                                                            (setq p0 (trans pt 0 nrm))
  91.                                                            (<= 0 (* (sin (angle p0 p-)) (sin (angle p0 p+)))) ;; LM Mod
  92.                                                       )
  93.                                                     )
  94.                                           ) lst
  95.                             )
  96.                     ) 2
  97.                )
  98.           )
  99.         )
  100.       )
  101.     )
  102.   )
  103.  
  104.   (defun processpla ( pla / ipl pla1 pla2 )
  105.     (setq pla1 pla)
  106.     (foreach p1 pla1
  107.       (setq pla2 (vl-remove p1 pla1))
  108.       (foreach p2 pla2
  109.         (setq ipl (cons (inters (car p1) (polar (car p1) (cadr p1) 1.0) (car p2) (polar (car p2) (cadr p2) 1.0) nil) ipl))
  110.       )
  111.     )
  112.     (setq ipl (vl-remove-if-not (function (lambda ( p ) (LM:Inside-p p lw))) (vl-remove nil ipl)))
  113.   )
  114.  
  115.   (defun distp2t ( p tt )
  116.     (distance p (inters p (polar p (+ (angle (car tt) (cadr tt)) (* 0.5 pi)) 1.0) (car tt) (cadr tt) nil))
  117.   )
  118.  
  119.   (defun removesingles ( l / a )
  120.     (while (setq a (vl-some (function (lambda ( x ) (if (= (1- (length l)) (length (vl-remove-if (function (lambda ( y ) (equal x y 1e-6))) l))) x))) l))
  121.       (setq l (vl-remove a l))
  122.     )
  123.     l
  124.   )
  125.  
  126.   (defun findipinterschilds ( ip pla / pla1 pla2 r )
  127.     (setq pla1 pla)
  128.     (foreach p1 pla1
  129.       (setq pla2 (vl-remove p1 pla1))
  130.       (foreach p2 pla2
  131.         (if (equal ip (inters (car p1) (polar (car p1) (cadr p1) 1.0) (car p2) (polar (car p2) (cadr p2) 1.0) nil) 1e-6)
  132.           (setq r (list p1 p2))
  133.         )
  134.       )
  135.     )
  136.     r
  137.   )
  138.  
  139.   (defun processtxtipl ( tl / ipl tl1 tl2 )
  140.     (setq tl1 tl)
  141.     (foreach t1 tl1
  142.       (setq tl2 (vl-remove t1 tl1))
  143.       (foreach t2 tl2
  144.         (setq ipl (cons (inters (car t1) (polar (car t1) (angle (car t1) (cadr t1)) 1.0) (car t2) (polar (car t2) (angle (car t2) (cadr t2)) 1.0) nil) ipl))
  145.       )
  146.     )
  147.     (if (vl-some (function null) ipl)
  148.       (exit)
  149.       ipl
  150.     )
  151.   )
  152.  
  153.   (defun process nil
  154.     (if ip
  155.       (setq p1p2 (findipinterschilds ip pla))
  156.       (setq p1p2 nil)
  157.     )
  158.     (setq f nil)
  159.     (if p1p2
  160.       (progn
  161.         (setq tt (vl-some (function (lambda ( x ) (if (and (assoc (car x) p1p2) (assoc (cadr x) p1p2)) (progn (setq f t) x)))) tlo))
  162.         (if
  163.           (or
  164.             (vl-some (function (lambda ( x ) (equal (distance ip (car (car p1p2))) (+ (distance ip x) (distance x (car (car p1p2)))) 1e-6))) (vl-remove-if (function (lambda ( x ) (equal x (car (car p1p2)) 1e-6))) (vl-remove-if (function (lambda ( x ) (equal x ip 1e-6))) (mapcar (function car) ipldtl))))
  165.             (vl-some (function (lambda ( x ) (equal (distance ip (car (cadr p1p2))) (+ (distance ip x) (distance x (car (cadr p1p2)))) 1e-6))) (vl-remove-if (function (lambda ( x ) (equal x (car (cadr p1p2)) 1e-6))) (vl-remove-if (function (lambda ( x ) (equal x ip 1e-6))) (mapcar (function car) ipldtl))))
  166.           )
  167.           (setq f nil)
  168.         )
  169.       )
  170.     )
  171.     f
  172.   )
  173.  
  174.   (defun _vl-position ( e l tol / car-vl-member-if )
  175.     (defun car-vl-member-if ( f l / ff r )
  176.       (setq ff (function (lambda ( x ) (if (apply f (list x)) (setq r x)))))
  177.       (vl-some ff l)
  178.       r
  179.     )
  180.     (vl-position (car-vl-member-if (function (lambda ( x ) (equal e x tol))) l) l)
  181.   )
  182.  
  183.   (defun car-sort ( lst cmp / rtn )
  184.     (setq rtn (car lst))
  185.     (foreach itm (cdr lst)
  186.       (if (apply cmp (list itm rtn))
  187.         (setq rtn itm)
  188.       )
  189.     )
  190.     rtn
  191.   )
  192.  
  193.   (defun car-vl-member-if ( f l / ff r )
  194.     (setq ff (function (lambda ( x ) (if (apply f (list x)) (setq r x)))))
  195.     (vl-some ff l)
  196.     r
  197.   )
  198.  
  199.  
  200. ;;; main 2droof ;;;
  201.  
  202.   (setq lw (car (entsel "\nPick a closed polygon : ")))
  203.   (setq ti (car (_vl-times)))
  204.   (setq n (cdr (assoc 90 (entget lw))))
  205.   (setq pl (mapcar (function cdr) (vl-remove-if (function (lambda ( x ) (/= (car x) 10))) (entget lw))))
  206.   (setq tl (mapcar (function (lambda ( a b ) (list a b))) pl (append (cdr pl) (list (car pl)))))
  207.   (setq tlo tl)
  208.   (setq pla (mapcar (function (lambda ( p mp ) (list p (angle p mp)))) pl (mapcar (function (lambda ( a b ) (if (clockwise-p (car a) (car b) (cadr b)) (mid (polar (cadr a) (angle (cadr a) (car a)) -1.0) (polar (car b) (angle (car b) (cadr b)) -1.0)) (mid (polar (cadr a) (angle (cadr a) (car a)) 1.0) (polar (car b) (angle (car b) (cadr b)) 1.0))))) (cons (last tl) tl) tl)))
  209.   (setq plaa pla)
  210.   (while (> n 0)
  211.     (if (null ipll)
  212.       (setq ipl (unique (processpla plaa)))
  213.       (setq ipl (unique (processpla (unique pla))))
  214.     )
  215.     (if (equal ipl iplo 1e-6)
  216.       (setq n 0)
  217.     )
  218.     (if fff
  219.       (progn
  220.         (setq pl (mapcar (function car) pla))
  221.         (foreach x pl
  222.           (setq lil (cons (list x ip) lil))
  223.         )
  224.         (setq n 0)
  225.       )
  226.       (progn
  227.         (setq ipldtl (mapcar (function (lambda ( p ) (list p (vl-sort (mapcar (function (lambda ( tt ) (distp2t p tt))) tlo) (function <))))) ipl))
  228.         (setq ipldtl (mapcar (function (lambda ( x ) (list (car x) (removesingles (cadr x))))) ipldtl))
  229.         (setq ipldtl (vl-remove-if (function (lambda ( x ) (< (- (length (cadr x)) (length (vl-remove-if (function (lambda ( y ) (equal y (car (cadr x)) 1e-6))) (cadr x)))) 3))) ipldtl))
  230.         (if ipll
  231.           (setq ipldtl (vl-remove-if (function (lambda ( x ) (_vl-position (car x) ipll 1e-6))) ipldtl))
  232.         )
  233.         (if (null ipll)
  234.           (setq ipldtlo ipldtl)
  235.         )
  236.         (setq ip (car (car-sort (unique (append ipldtl ipldtlo)) (function (lambda ( a b ) (< (car (cadr a)) (car (cadr b))))))))
  237.         (setq ipldtlo (vl-remove-if (function (lambda ( x ) (equal (car x) ip 1e-6))) ipldtlo))
  238.         (setq ipp nil)
  239.         (if (not (process))
  240.           (if ipll
  241.             (progn
  242.               (foreach y (reverse pal)
  243.                 (setq ipp (cons (vl-remove-if-not (function (lambda ( x ) (vl-some (function (lambda ( a ) (or (equal (rem (+ a pi) (* 2 pi)) (angle (car y) x) 1e-6) (equal a (angle (car y) x) 1e-6)))) (cadr y)))) (mapcar (function car) (vl-sort ipldtl (function (lambda ( a b ) (> (car (cadr a)) (car (cadr b)))))))) ipp))
  244.               )
  245.               (setq ipp (apply (function append) ipp))
  246.               (if p1p2
  247.                 (setq ipp (cons ip ipp))
  248.               )
  249.               (setq ipp (unique ipp))
  250.               (setq ipp (mapcar (function (lambda ( p ) (list p (findipinterschilds p pla)))) ipp))
  251.               (setq ipp (vl-remove-if (function (lambda ( x ) (null (cadr x)))) ipp))
  252.               (setq ipp (vl-remove-if (function (lambda ( x )
  253.                 (or
  254.                   (vl-some (function (lambda ( y ) (equal (distance (car x) (car (car (cadr x)))) (+ (distance (car x) y) (distance y (car (car (cadr x))))) 1e-6))) (vl-remove-if (function (lambda ( z ) (equal z (car (car (cadr x))) 1e-6))) (vl-remove-if (function (lambda ( z ) (equal z (car x) 1e-6))) (mapcar (function car) ipldtl))))
  255.                   (vl-some (function (lambda ( y ) (equal (distance (car x) (car (cadr (cadr x)))) (+ (distance (car x) y) (distance y (car (cadr (cadr x))))) 1e-6))) (vl-remove-if (function (lambda ( z ) (equal z (car (cadr (cadr x))) 1e-6))) (vl-remove-if (function (lambda ( z ) (equal z (car x) 1e-6))) (mapcar (function car) ipldtl))))
  256.                   (vl-some (function (lambda ( y / ii ) (and (setq ii (inters (car x) (car (car (cadr x))) (car y) (cadr y) t)) (not (equal ii (car x) 1e-6)) (not (equal ii (car (car (cadr x))) 1e-6)) (not (equal ii (car y) 1e-6)) (not (equal ii (cadr y) 1e-6))))) (append tlo lil))
  257.                   (vl-some (function (lambda ( y / ii ) (and (setq ii (inters (car x) (car (cadr (cadr x))) (car y) (cadr y) t)) (not (equal ii (car x) 1e-6)) (not (equal ii (car (cadr (cadr x))) 1e-6)) (not (equal ii (car y) 1e-6)) (not (equal ii (cadr y) 1e-6))))) (append tlo lil))
  258.                 ))) ipp)
  259.               )
  260.               (setq dl (mapcar (function (lambda ( y ) (car (cadr (car-vl-member-if (function (lambda ( x ) (equal (car x) y 1e-6))) ipldtl))))) (mapcar (function car) ipp)))
  261.               (if dl
  262.                 (setq ip (car (nth (vl-position (car-sort dl (function <)) dl) ipp)))
  263.               )
  264.             )
  265.           )
  266.         )
  267.         (if (vl-every (function (lambda ( x ) (equal ip (inters (car (car pla)) (polar (car (car pla)) (cadr (car pla)) 1.0) (car x) (polar (car x) (cadr x) 1.0) nil) 1e-6))) (cdr pla))
  268.           (setq fff t)
  269.         )
  270.         (setq d (car (cadr (car-vl-member-if (function (lambda ( x ) (equal (car x) ip 1e-6))) (unique (append ipldtl ipldtlo))))))
  271.         (setq al nil a nil)
  272.         (process)
  273.         (if (null p1p2)
  274.           (setq ip nil)
  275.         )
  276.         (if (and tt f)
  277.           (progn
  278.             (setq tl (vl-remove tt tl))
  279.             (setq n (1- n))
  280.           )
  281.         )
  282.         (if f
  283.           (progn
  284.             (setq t1 (vl-some (function (lambda ( x ) (if (or (assoc (car x) (list (car p1p2))) (assoc (cadr x) (list (car p1p2)))) x))) tl))
  285.             (setq t2 (vl-some (function (lambda ( x ) (if (or (assoc (car x) (list (cadr p1p2))) (assoc (cadr x) (list (cadr p1p2)))) x))) tl))
  286.           )
  287.           (setq t1 nil t2 nil)
  288.         )
  289.         (if ip
  290.           (setq ipll (cons ip ipll))
  291.         )
  292.         (if (and t1 t2 ip)
  293.           (progn
  294.             (if (null (setq itt (inters (car t1) (cadr t1) (car t2) (cadr t2) nil)))
  295.               (exit)
  296.               (setq a (angle itt ip))
  297.             )
  298.             (if a
  299.               (progn
  300.                 (setq pla (cons (list ip a) pla))
  301.                 (setq pla (vl-remove (car p1p2) pla) pla (vl-remove (cadr p1p2) pla))
  302.                 (setq lil (cons (list (car (car p1p2)) ip) lil))
  303.                 (setq lil (cons (list (car (cadr p1p2)) ip) lil))
  304.                 (setq pal (cons (list ip (list a)) pal))
  305.               )
  306.             )
  307.           )
  308.           (if (and ip (null fff))
  309.             (progn
  310.               (setq lil (cons (list (car (car p1p2)) ip) lil))
  311.               (setq lil (cons (list (car (cadr p1p2)) ip) lil))
  312.               (if (setq p (vl-some (function (lambda ( x ) (if (vl-position x plaa) x))) p1p2))
  313.                 (progn
  314.                   (setq n (1- n))
  315.                   (setq tll (vl-remove-if-not (function (lambda ( x ) (vl-some (function (lambda ( y ) (_vl-position p y 1e-6))) x))) tlo))
  316.                   (setq tll (car (vl-remove-if-not (function (lambda ( x ) (and (_vl-position (car x) (vl-remove nil (mapcar (function car) lil)) 1e-6) (_vl-position (cadr x) (vl-remove nil (mapcar (function car) lil)) 1e-6)))) tll)))
  317.                   (setq tl (vl-remove tll tl))
  318.                 )
  319.               )
  320.               (setq tll (vl-remove-if-not (function (lambda ( x ) (equal d (distp2t ip x) 1e-6))) tl))
  321.               (setq txtipl (processtxtipl tll))
  322.               (setq txtipl (vl-remove-if (function (lambda ( x ) (_vl-position x pl 1e-6))) txtipl))
  323.               (setq al (mapcar (function (lambda ( p ) (angle p ip))) txtipl))
  324.               (setq al (vl-remove-if (function (lambda ( x ) (_vl-position x (mapcar (function cadr) p1p2) 1e-6))) al))
  325.               (setq al (unique al))
  326.               (if al
  327.                 (progn
  328.                   (setq pla (append pla (mapcar (function (lambda ( a ) (list ip a))) al)))
  329.                   (setq pal (cons (list ip al) pal))
  330.                 )
  331.               )
  332.               (setq pla (vl-remove (car p1p2) pla) pla (vl-remove (cadr p1p2) pla))
  333.             )
  334.           )
  335.         )
  336.       )
  337.     )
  338.     (setq iplo ipl)
  339.   )
  340.   (setq lil (vl-remove-if (function (lambda ( x ) (equal (car x) (cadr x) 1e-6))) lil))
  341.   (setq lil (unique lil))
  342.   (setq lil (vl-remove-if (function (lambda ( x ) (vl-some (function (lambda ( y ) (null y))) x))) lil))
  343.   (setq lipl (apply (function append) lil))
  344.   (setq lipl (unique (vl-remove-if (function (lambda ( x ) (or (= (length (vl-remove x lipl)) (1- (length lipl))) (= (length (vl-remove x lipl)) (- (length lipl) 3))))) lipl)))
  345.   (if lipl
  346.     (setq lil (cons (list (car lipl) (cadr lipl)) lil))
  347.   )
  348.   (foreach li lil
  349.     (entmake (list '(0 . "LINE") (cons 10 (car li)) (cons 11 (cadr li))))
  350.   )
  351.   (prompt "\nElapsed time : ") (princ (rtos (- (car (_vl-times)) ti) 2 20)) (prompt " milliseconds...")
  352.   (princ)
  353. )
  354.  

(https://serving.photos.photobox.com/30182870659ff90949a2859a52275dda1b1e2d7ab23410bec056cec8f3fd45002e9c1a97.jpg)

Regards, and Happy New 2021. Year...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: BIGAL on January 07, 2021, 06:16:02 PM
Wow pretty fantastic shape to work out a roof, its like the AUS opera house they said it could not be built.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 08, 2021, 09:31:46 AM
I've tried to implement orthogonality in solution, but strangely the same rule can't be applied to this cases... Still not to be that I didn't try, here is my revision - please use better solution - previously posted code if you don't have orthogonality...

Code removed due to later revisions that are better...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 08, 2021, 12:01:02 PM
I solved it... It should work on any case, just like it should...
Code - Auto/Visual Lisp: [Select]
  1. (defun c:2droof (/ unique mid clockwise-p LM:Inside-p processpla distp2t removesingles removedoubles findipinterschilds processtxtipl process unioncollinearplaneprints _vl-position car-sort car-vl-member-if lw ti n pl tl tlo utlo pla plaa ipl ipll ipldtl ipldtlo ip p1p2 tt t1 t2 a f lil txtipl tll d al iplo p pal fff ipp lipl dl ipo itt)
  2.  
  3.   (defun unique (l)
  4.     (if l (cons (car l) (unique (vl-remove-if (function (lambda (x) (equal x (car l) 1e-6))) l))))
  5.   )
  6.   (defun mid (p1 p2)
  7.     (mapcar (function (lambda (a b) (/ (+ a b) 2.0))) p1 p2)
  8.   )
  9.   (defun clockwise-p (p1 p2 p3)
  10.     (minusp (sin (- (angle p1 p3) (angle p1 p2))))
  11.   )
  12.   (defun LM:Inside-p (pt ent / unit v^v _GroupByNum fd1 fd2 par lst nrm obj tmp)
  13.     (vl-load-com)
  14.     (defun unit (v / d)
  15.       (if (not (equal (setq d (distance '(0.0 0.0 0.0) v)) 0.0 1e-8))
  16.         (mapcar (function (lambda (x) (/ x d))) v)
  17.       )
  18.     )
  19.     (defun v^v (u v)
  20.       (list
  21.         (- (* (cadr u) (caddr v)) (* (caddr u) (cadr v)))
  22.         (- (* (caddr u) (car v)) (* (car u) (caddr v)))
  23.         (- (* (car u) (cadr v)) (* (cadr u) (car v)))
  24.       )
  25.     )
  26.     (defun _GroupByNum (l n / r)
  27.       (if l
  28.         (cons (reverse (repeat n (setq r (cons (car l) r) l (cdr l)) r)) (_GroupByNum l n))
  29.       )
  30.     )
  31.     (if (= (type ent) 'VLA-OBJECT)
  32.       (setq obj ent ent (vlax-vla-object->ename ent))
  33.       (setq obj (vlax-ename->vla-object ent))
  34.     )
  35.     (if (vlax-curve-isplanar ent)
  36.       (progn
  37.         (setq fd1 (vlax-curve-getfirstderiv ent (setq par (vlax-curve-getstartparam ent))))
  38.         (while (or (equal fd1 (setq fd2 (vlax-curve-getfirstderiv ent (setq par (+ par 0.001)))) 1e-3) (null (setq nrm (unit (v^v fd1 fd2))))))
  39.         (setq lst
  40.           (_GroupByNum
  41.             (vlax-invoke
  42.               (setq tmp
  43.                 (vlax-ename->vla-object
  44.                   (entmakex
  45.                     (list
  46.                       (cons 0 "RAY")
  47.                       (cons 100 "AcDbEntity")
  48.                       (cons 100 "AcDbRay")
  49.                       (cons 10 pt)
  50.                       (cons 11 (trans '(1. 0. 0.) nrm 0))
  51.                     )
  52.                   )
  53.                 )
  54.               )
  55.               'IntersectWith obj acextendnone
  56.             ) 3
  57.           )
  58.         )
  59.         (vla-delete tmp)
  60.         ;; gile:
  61.         (and
  62.           lst
  63.           (not (vlax-curve-getparamatpoint ent pt))
  64.           (= 1 (rem (length (vl-remove-if (function (lambda (p / pa p- p+ p0)
  65.                                                       (setq pa (vlax-curve-getparamatpoint ent p))
  66.                                                       (and (setq p- (cond ((setq p- (vlax-curve-getPointatParam ent (- pa 1e-8)))
  67.                                                                            (trans p- 0 nrm)
  68.                                                                           )
  69.                                                                           ((trans (vlax-curve-getPointatParam ent (- (vlax-curve-getEndParam ent) 1e-8)) 0 nrm)
  70.                                                                           )
  71.                                                                     )
  72.                                                            )
  73.                                                            (setq p+ (cond ((setq p+ (vlax-curve-getPointatParam ent (+ pa 1e-8)))
  74.                                                                            (trans p+ 0 nrm)
  75.                                                                           )
  76.                                                                           ((trans (vlax-curve-getPointatParam ent (+ (vlax-curve-getStartParam ent) 1e-8)) 0 nrm)
  77.                                                                           )
  78.                                                                     )
  79.                                                            )
  80.                                                            (setq p0 (trans pt 0 nrm))
  81.                                                            (<= 0 (* (sin (angle p0 p-)) (sin (angle p0 p+)))) ;; LM Mod
  82.                                                       )
  83.                                                     )
  84.                                           ) lst
  85.                             )
  86.                     ) 2
  87.                )
  88.           )
  89.         )
  90.       )
  91.     )
  92.   )
  93.   (defun processpla (pla / ipl pla1 pla2)
  94.     (setq pla1 pla)
  95.     (foreach p1 pla1
  96.       (setq pla2 (vl-remove p1 pla1))
  97.       (foreach p2 pla2
  98.         (setq ipl (cons (inters (car p1) (polar (car p1) (cadr p1) 1.0) (car p2) (polar (car p2) (cadr p2) 1.0) nil) ipl))
  99.       )
  100.     )
  101.     (setq ipl (vl-remove-if-not (function (lambda (p) (LM:Inside-p p lw))) (vl-remove nil ipl)))
  102.   )
  103.   (defun distp2t (p tt)
  104.     (distance p (inters p (polar p (+ (angle (car tt) (cadr tt)) (* 0.5 pi)) 1.0) (car tt) (cadr tt) nil))
  105.   )
  106.   (defun removesingles (l / a)
  107.     (while (setq a (vl-some (function (lambda (x) (if (= (1- (length l)) (length (vl-remove-if (function (lambda (y) (equal x y 1e-6))) l))) x))) l))
  108.       (setq l (vl-remove-if (function (lambda (x) (equal x a 1e-6))) l))
  109.     )
  110.     l
  111.   )
  112.   (defun removedoubles (l / a)
  113.     (while (setq a (vl-some (function (lambda (x) (if (= (- (length l) 2) (length (vl-remove-if (function (lambda (y) (equal x y 1e-6))) l))) x))) l))
  114.       (setq l (vl-remove-if (function (lambda (x) (equal x a 1e-6))) l))
  115.     )
  116.     l
  117.   )
  118.   (defun findipinterschilds (ip pla / pla1 pla2 r)
  119.     (setq pla1 pla)
  120.     (foreach p1 pla1
  121.       (setq pla2 (vl-remove p1 pla1))
  122.       (foreach p2 pla2
  123.         (if (equal ip (inters (car p1) (polar (car p1) (cadr p1) 1.0) (car p2) (polar (car p2) (cadr p2) 1.0) nil) 1e-6)
  124.           (setq r (list p1 p2))
  125.         )
  126.       )
  127.     )
  128.     r
  129.   )
  130.   (defun processtxtipl (tl / ipl tl1 tl2 intt)
  131.     (setq tl1 tl)
  132.     (foreach t1 tl1
  133.       (setq tl2 (vl-remove t1 tl1))
  134.       (foreach t2 tl2
  135.         (if (setq intt (inters (car t1) (polar (car t1) (angle (car t1) (cadr t1)) 1.0) (car t2) (polar (car t2) (angle (car t2) (cadr t2)) 1.0) nil))
  136.           (setq ipl (cons intt ipl))
  137.           (setq ipl (cons (angle (car t1) (cadr t1)) ipl))
  138.         )
  139.       )
  140.     )
  141.     ipl
  142.   )
  143.   (defun process nil
  144.     (if ip
  145.       (setq p1p2 (findipinterschilds ip pla))
  146.       (setq p1p2 nil)
  147.     )
  148.     (setq f nil)
  149.     (if p1p2
  150.       (progn
  151.         (setq tt (vl-some (function (lambda (x) (if (and (assoc (car x) p1p2) (assoc (cadr x) p1p2)) (progn (setq f t) x)))) tlo))
  152.         (if
  153.           (or
  154.             (vl-some (function (lambda (x) (equal (distance ip (car (car p1p2))) (+ (distance ip x) (distance x (car (car p1p2)))) 1e-6))) (vl-remove-if (function (lambda (x) (equal x (car (car p1p2)) 1e-6))) (vl-remove-if (function (lambda (x) (equal x ip 1e-6))) (mapcar (function car) ipldtl))))
  155.             (vl-some (function (lambda (x) (equal (distance ip (car (cadr p1p2))) (+ (distance ip x) (distance x (car (cadr p1p2)))) 1e-6))) (vl-remove-if (function (lambda (x) (equal x (car (cadr p1p2)) 1e-6))) (vl-remove-if (function (lambda (x) (equal x ip 1e-6))) (mapcar (function car) ipldtl))))
  156.             (vl-some (function (lambda (y / ii) (and (setq ii (inters ip (car (car p1p2)) (car y) (cadr y) t)) (not (equal ii ip 1e-6)) (not (equal ii (car (car p1p2)) 1e-6)) (not (equal ii (car y) 1e-6)) (not (equal ii (cadr y) 1e-6))))) (append tlo lil))
  157.             (vl-some (function (lambda (y / ii) (and (setq ii (inters ip (car (cadr p1p2)) (car y) (cadr y) t)) (not (equal ii ip 1e-6)) (not (equal ii (car (cadr p1p2)) 1e-6)) (not (equal ii (car y) 1e-6)) (not (equal ii (cadr y) 1e-6))))) (append tlo lil))
  158.           )
  159.           (setq f nil)
  160.         )
  161.       )
  162.     )
  163.     f
  164.   )
  165.   (defun unioncollinearplaneprints (tl / a b tll)
  166.     (while (setq a (car tl))
  167.       (setq b (vl-remove-if-not (function (lambda (x) (and (or (equal (angle (car a) (cadr a)) (angle (car a) (car x)) 1e-6) (equal (angle (cadr a) (car a)) (angle (car a) (car x)) 1e-6)) (or (equal (angle (car a) (cadr a)) (angle (car a) (cadr x)) 1e-6) (equal (angle (cadr a) (car a)) (angle (car a) (cadr x)) 1e-6))))) tl))
  168.       (setq tll (cons a tll))
  169.       (if b
  170.         (setq tl (vl-remove-if (function (lambda (x) (vl-position x b))) tl))
  171.         (setq tl (cdr tl))
  172.       )
  173.     )
  174.     tll
  175.   )
  176.   (defun car-vl-member-if (f l / ff r)
  177.     (setq ff (function (lambda (x) (if (apply f (list x)) (setq r x)))))
  178.     (vl-some ff l)
  179.     r
  180.   )
  181.   (defun _vl-position (e l tol)
  182.     (vl-position (car-vl-member-if (function (lambda ( x ) (equal e x tol))) l) l)
  183.   )
  184.   (defun car-sort (lst cmp / rtn)
  185.     (setq rtn (car lst))
  186.     (foreach itm (cdr lst)
  187.       (if (apply cmp (list itm rtn))
  188.         (setq rtn itm)
  189.       )
  190.     )
  191.     rtn
  192.   )
  193. ;;; main 2droof ;;;
  194.   (if (setq lw (car (entsel "\nPick a closed polygonal LWPOLYLINE...")))
  195.     (progn
  196.       (setq ti (car (_vl-times)))
  197.       (gc)
  198.       (setq n (cdr (assoc 90 (entget lw))))
  199.       (setq pl (mapcar (function cdr) (vl-remove-if (function (lambda (x) (/= (car x) 10))) (entget lw))))
  200.       (setq tl (mapcar (function (lambda (a b) (list a b))) pl (append (cdr pl) (list (car pl)))))
  201.       (setq tlo tl)
  202.       (setq utlo (unioncollinearplaneprints tlo))
  203.       (setq pla (mapcar (function (lambda (p mp) (list p (angle p mp)))) pl (mapcar (function (lambda (a b) (if (clockwise-p (car a) (car b) (cadr b)) (mid (polar (cadr a) (angle (cadr a) (car a)) -1.0) (polar (car b) (angle (car b) (cadr b)) -1.0)) (mid (polar (cadr a) (angle (cadr a) (car a)) 1.0) (polar (car b) (angle (car b) (cadr b)) 1.0))))) (cons (last tl) tl) tl)))
  204.       (setq plaa pla)
  205.       (while (> n 0)
  206.         (if (null ipll)
  207.           (setq ipl (unique (processpla plaa)))
  208.           (setq ipl (unique (processpla (unique pla))))
  209.         )
  210.         (if (equal ipl iplo 1e-6)
  211.           (setq n 0)
  212.         )
  213.         (if fff
  214.           (progn
  215.             (setq pl (mapcar (function car) pla))
  216.             (foreach x pl
  217.               (setq lil (cons (list x ip) lil))
  218.             )
  219.             (setq n 0)
  220.           )
  221.           (progn
  222.             (setq ipo ip)
  223.             (setq ipldtl (mapcar (function (lambda (p) (list p (vl-sort (mapcar (function (lambda (tt) (distp2t p tt))) utlo) (function <))))) ipl))
  224.             (setq ipldtl (mapcar (function (lambda (x) (list (car x) (removedoubles (removesingles (cadr x)))))) ipldtl))
  225.             (setq ipldtl (vl-remove-if (function (lambda (x) (null (cadr x)))) ipldtl))
  226.             (if ipll
  227.               (setq ipldtl (vl-remove-if (function (lambda (x) (_vl-position (car x) ipll 1e-6))) ipldtl))
  228.             )
  229.             (setq ipldtl (mapcar (function (lambda (x) (list (car x) (vl-sort (cadr x) (function <))))) ipldtl))
  230.             (setq ipldtlo (vl-sort ipldtl (function (lambda (a b) (< (car (cadr a)) (car (cadr b)))))))
  231.             (setq ipldtlo (vl-remove-if (function (lambda (x) (_vl-position (car x) ipll 1e-6))) ipldtlo))
  232.             (setq ip (car (car ipldtlo)))
  233.             (while (and (not (process)) ipldtlo)
  234.               (setq ipldtlo (cdr ipldtlo))
  235.               (setq ip (car (car ipldtlo)))
  236.             )
  237.             (setq ipp nil)
  238.             (if ipll
  239.               (progn
  240.                 (foreach y (reverse pal)
  241.                   (setq ipp (cons (vl-remove-if-not (function (lambda (x) (vl-some (function (lambda (a) (or (equal (rem (+ a pi) (* 2 pi)) (angle (car y) x) 1e-6) (equal a (angle (car y) x) 1e-6)))) (cadr y)))) (mapcar (function car) (vl-remove-if (function (lambda (x) (_vl-position (car x) ipll 1e-6))) ipldtl))) ipp))
  242.                 )
  243.                 (setq ipp (apply (function append) ipp))
  244.                 (if (and ip p1p2)
  245.                   (setq ipp (cons ip ipp))
  246.                 )
  247.                 (setq ipp (unique ipp))
  248.                 (setq ipp (mapcar (function (lambda (p) (list p (findipinterschilds p pla)))) ipp))
  249.                 (setq ipp (vl-remove-if (function (lambda (x) (null (cadr x)))) ipp))
  250.                 (setq ipp (vl-remove-if (function (lambda (x)
  251.                   (or
  252.                     (vl-some (function (lambda (y) (equal (distance (car x) (car (car (cadr x)))) (+ (distance (car x) y) (distance y (car (car (cadr x))))) 1e-6))) (vl-remove-if (function (lambda (z) (equal z (car (car (cadr x))) 1e-6))) (vl-remove-if (function (lambda (z) (equal z (car x) 1e-6))) (mapcar (function car) ipldtl))))
  253.                     (vl-some (function (lambda (y) (equal (distance (car x) (car (cadr (cadr x)))) (+ (distance (car x) y) (distance y (car (cadr (cadr x))))) 1e-6))) (vl-remove-if (function (lambda (z) (equal z (car (cadr (cadr x))) 1e-6))) (vl-remove-if (function (lambda (z) (equal z (car x) 1e-6))) (mapcar (function car) ipldtl))))
  254.                     (vl-some (function (lambda (y / ii) (and (setq ii (inters (car x) (car (car (cadr x))) (car y) (cadr y) t)) (not (equal ii (car x) 1e-6)) (not (equal ii (car (car (cadr x))) 1e-6)) (not (equal ii (car y) 1e-6)) (not (equal ii (cadr y) 1e-6))))) (append tlo lil))
  255.                     (vl-some (function (lambda (y / ii) (and (setq ii (inters (car x) (car (cadr (cadr x))) (car y) (cadr y) t)) (not (equal ii (car x) 1e-6)) (not (equal ii (car (cadr (cadr x))) 1e-6)) (not (equal ii (car y) 1e-6)) (not (equal ii (cadr y) 1e-6))))) (append tlo lil))
  256.                   )
  257.                   )) ipp)
  258.                 )
  259.                 (setq dl (mapcar (function (lambda (y) (car (cadr (car-vl-member-if (function (lambda (x) (equal (car x) y 1e-6))) ipldtl))))) (mapcar (function car) ipp)))
  260.                 (if dl
  261.                   (setq ip (car (nth (vl-position (car-sort dl (function <)) dl) ipp)))
  262.                 )
  263.               )
  264.             )
  265.             (if (vl-every (function (lambda (x) (equal ip (inters (car (car pla)) (polar (car (car pla)) (cadr (car pla)) 1.0) (car x) (polar (car x) (cadr x) 1.0) nil) 1e-6))) (cdr pla))
  266.               (setq fff t)
  267.             )
  268.             (process)
  269.             (if (and p1p2 (setq itt (vl-some (function (lambda (x) (if (or (equal (distance ip (car (car p1p2))) (+ (distance ip (car x)) (distance (car x) (car (car p1p2)))) 1e-6) (equal (distance ip (car (cadr p1p2))) (+ (distance ip (car x)) (distance (car x) (car (cadr p1p2)))) 1e-6)) (car x)))) (vl-remove-if (function (lambda (x) (equal ip (car x) 1e-6))) ipp))))
  270.               (progn
  271.                 (setq ip itt)
  272.                 (process)
  273.               )
  274.             )
  275.             (if (equal ipo ip 1e-6)
  276.               (setq n 0)
  277.             )
  278.             (setq d (car (cadr (car-vl-member-if (function (lambda (x) (equal (car x) ip 1e-6))) ipldtl))))
  279.             (setq al nil a nil)
  280.             (if (and tt f)
  281.               (setq tl (vl-remove tt tl))
  282.             )
  283.             (if f
  284.               (progn
  285.                 (setq t1 (vl-some (function (lambda (x) (if (or (assoc (car x) (list (car p1p2))) (assoc (cadr x) (list (car p1p2)))) x))) tl))
  286.                 (setq t2 (vl-some (function (lambda (x) (if (or (assoc (car x) (list (cadr p1p2))) (assoc (cadr x) (list (cadr p1p2)))) x))) tl))
  287.               )
  288.               (setq t1 nil t2 nil)
  289.             )
  290.             (if ip
  291.               (setq ipll (cons ip ipll))
  292.             )
  293.             (if (and t1 t2 ip)
  294.               (progn
  295.                 (if (inters (car t1) (cadr t1) (car t2) (cadr t2) nil)
  296.                   (setq a (angle (inters (car t1) (cadr t1) (car t2) (cadr t2) nil) ip))
  297.                   (setq a (angle (car t1) (cadr t1)))
  298.                 )
  299.                 (if a
  300.                   (progn
  301.                     (setq pla (cons (list ip a) pla))
  302.                     (setq pla (vl-remove-if (function (lambda (x) (equal (car x) (car (car p1p2)) 1e-6))) pla) pla (vl-remove-if (function (lambda (x) (equal (car x) (car (cadr p1p2)) 1e-6))) pla))
  303.                     (if (car (car p1p2))
  304.                       (setq lil (cons (list (car (car p1p2)) ip) lil))
  305.                     )
  306.                     (if (car (cadr p1p2))
  307.                       (setq lil (cons (list (car (cadr p1p2)) ip) lil))
  308.                     )
  309.                     (setq pal (cons (list ip (list a)) pal))
  310.                   )
  311.                 )
  312.               )
  313.               (if (and ip (null fff))
  314.                 (progn
  315.                   (if (car (car p1p2))
  316.                     (setq lil (cons (list (car (car p1p2)) ip) lil))
  317.                   )
  318.                   (if (car (cadr p1p2))
  319.                     (setq lil (cons (list (car (cadr p1p2)) ip) lil))
  320.                   )
  321.                   (if (setq p (vl-some (function (lambda (x) (if (vl-position x plaa) x))) p1p2))
  322.                     (progn
  323.                       (setq tll (vl-remove-if-not (function (lambda (x) (vl-some (function (lambda (y) (_vl-position p y 1e-6))) x))) tlo))
  324.                       (setq tll (car (vl-remove-if-not (function (lambda (x) (and (_vl-position (car x) (vl-remove nil (mapcar (function car) lil)) 1e-6) (_vl-position (cadr x) (vl-remove nil (mapcar (function car) lil)) 1e-6)))) tll)))
  325.                       (setq tl (vl-remove tll tl))
  326.                     )
  327.                   )
  328.                   (setq tll (vl-remove-if-not (function (lambda (x) (equal d (distp2t ip x) 1e-6))) tl))
  329.                   (setq txtipl (processtxtipl tll))
  330.                   (setq txtipl (vl-remove-if (function (lambda (x) (_vl-position x pl 1e-6))) txtipl))
  331.                   (setq al (mapcar (function (lambda (p) (if (listp p) (angle p ip) p))) txtipl))
  332.                   (setq al (vl-remove-if (function (lambda (x) (or (_vl-position x (mapcar (function cadr) p1p2) 1e-6) (_vl-position (rem (+ x pi) (* 2 pi)) (mapcar (function cadr) p1p2) 1e-6)))) al))
  333.                   (setq al (unique al))
  334.                   (if al
  335.                     (progn
  336.                       (setq pla (append (mapcar (function (lambda (a) (list ip a))) al) pla))
  337.                       (setq pal (cons (list ip al) pal))
  338.                     )
  339.                   )
  340.                   (setq pla (vl-remove-if (function (lambda (x) (equal (car x) (car (car p1p2)) 1e-6))) pla) pla (vl-remove-if (function (lambda (x) (equal (car x) (car (cadr p1p2)) 1e-6))) pla))
  341.                 )
  342.               )
  343.             )
  344.           )
  345.         )
  346.         (setq iplo ipl)
  347.       )
  348.       (setq lil (vl-remove-if (function (lambda (x) (equal (car x) (cadr x) 1e-6))) lil))
  349.       (setq lil (unique lil))
  350.       (setq lil (vl-remove-if (function (lambda (x) (vl-some (function (lambda (y) (null y))) x))) lil))
  351.       (setq lipl (apply (function append) lil))
  352.       (setq lipl (unique (vl-remove-if (function (lambda (x) (or (= (length (vl-remove x lipl)) (1- (length lipl))) (= (length (vl-remove x lipl)) (- (length lipl) 3))))) lipl)))
  353.       (setq lipl (vl-remove-if (function (lambda (x) (vl-some (function (lambda (li) (and (equal (distance (car li) (cadr li)) (+ (distance (car li) x) (distance x (cadr li))) 1e-6) (not (equal x (car li) 1e-6)) (not (equal x (cadr li) 1e-6))))) lil))) lipl))
  354.       (if lipl
  355.         (setq lil (cons (list (car lipl) (cadr lipl)) lil))
  356.       )
  357.       (setq lil (vl-remove-if (function (lambda (x) (or (null (car x)) (null (cadr x))))) lil))
  358.       (foreach li lil
  359.         (entmake (list '(0 . "LINE") (cons 10 (car li)) (cons 11 (cadr li))))
  360.       )
  361.       (gc)
  362.       (prompt "\nElapsed time : ") (princ (rtos (- (car (_vl-times)) ti) 2 20)) (prompt " milliseconds...")
  363.     )
  364.   )
  365.   (princ)
  366. )
  367.  
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: d2010 on January 08, 2021, 01:42:11 PM
Wow pretty fantastic shape to work out a roof, its like the AUS opera house they said it could not be built.
Code - Auto/Visual Lisp: [Select]
  1. ;;;
  2. (defun c:2droof ( / nn_unique nn_mid nn_clockwisep nn_insidep nn_processpla nn_distp2t nn_removesingles nn_findipinterschilds nn_processtxtipl nn_prefetch nn_positionvl nn_carsort nn_car_vlmember_if lw ti n pl tl tlo pla plaa ipl ipll ipldtl ipldtlo ip p1p2 tt t1 t2 a f lil txtipl tll d al iplo p pal fff ipp lipl ippp dl )
  3.  
  4.   (defun nn_unique ( l )
  5.     (if l (cons (car l) (nn_unique (vl-remove-if (function (lambda ( x ) (equal x (car l) 1e-6))) l))))
  6.   )
  7.  
  8.   (defun nn_mid ( p1 p2 )
  9.     (mapcar (function (lambda ( a b ) (/ (+ a b) 2.0))) p1 p2)
  10.   )
  11.  
  12.   (defun nn_clockwisep ( p1 p2 p3 )
  13.     (minusp (sin (- (angle p1 p3) (angle p1 p2))))
  14.   )
  15.  
  16.   (defun nn_insidep ( pt ent / nn_unit nn_pmuld nn_groupbynum fd1 fd2 par lst nrm obj tmp )
  17.  
  18.     (vl-load-com)
  19.  
  20.     (defun nn_unit ( v / d )
  21.       (if (not (equal (setq d (distance '(0.0 0.0 0.0) v)) 0.0 1e-8))
  22.         (mapcar (function (lambda ( x ) (/ x d))) v)
  23.       )
  24.     )
  25.  
  26.     (defun nn_pmuld ( u v )
  27.       (list
  28.         (- (* (cadr u) (caddr v)) (* (caddr u) (cadr v)))
  29.         (- (* (caddr u) (car v)) (* (car u) (caddr v)))
  30.         (- (* (car u) (cadr v)) (* (cadr u) (car v)))
  31.       )
  32.     )
  33.  
  34.     (defun nn_groupbynum ( l n / r )
  35.       (if l
  36.         (cons (reverse (repeat n (setq r (cons (car l) r) l (cdr l)) r)) (nn_groupbynum l n))
  37.       )
  38.     )
  39.  
  40.     (if (= (type ent) 'VLA-OBJECT)
  41.       (setq obj ent
  42.             ent (vlax-vla-object->ename ent))
  43.       (setq obj (vlax-ename->vla-object ent))
  44.     )
  45.  
  46.     (if (vlax-curve-isplanar ent)
  47.       (progn
  48.         (setq fd1 (vlax-curve-getfirstderiv ent (setq par (vlax-curve-getstartparam ent))))
  49.         (while (or (equal fd1 (setq fd2 (vlax-curve-getfirstderiv ent (setq par (+ par 0.001)))) 1e-3) (null (setq nrm (nn_unit (nn_pmuld fd1 fd2))))))
  50.         (setq lst
  51.           (nn_groupbynum
  52.             (vlax-invoke
  53.               (setq tmp
  54.                 (vlax-ename->vla-object
  55.                   (entmakex
  56.                     (list
  57.                       (cons 0 "RAY")
  58.                       (cons 100 "AcDbEntity")
  59.                       (cons 100 "AcDbRay")
  60.                       (cons 10 pt)
  61.                       (cons 11 (trans '(1. 0. 0.) nrm 0))
  62.                     )
  63.                   )
  64.                 )
  65.               )
  66.               'IntersectWith obj acextendnone
  67.             ) 3
  68.           )
  69.         )
  70.         (vla-delete tmp)
  71.         ;; gile:
  72.         (and
  73.           lst
  74.           (not (vlax-curve-getparamatpoint ent pt))
  75.           (= 1 (rem (length (vl-remove-if (function (lambda ( p / pa p- p+ p0 )
  76.                                                       (setq pa (vlax-curve-getparamatpoint ent p))
  77.                                                       (and (setq p- (cond ((setq p- (vlax-curve-getPointatParam ent (- pa 1e-8)))
  78.                                                                            (trans p- 0 nrm)
  79.                                                                           )
  80.                                                                           ((trans (vlax-curve-getPointatParam ent (- (vlax-curve-getEndParam ent) 1e-8)) 0 nrm)
  81.                                                                           )
  82.                                                                     )
  83.                                                            )
  84.                                                            (setq p+ (cond ((setq p+ (vlax-curve-getPointatParam ent (+ pa 1e-8)))
  85.                                                                            (trans p+ 0 nrm)
  86.                                                                           )
  87.                                                                           ((trans (vlax-curve-getPointatParam ent (+ (vlax-curve-getStartParam ent) 1e-8)) 0 nrm)
  88.                                                                           )
  89.                                                                     )
  90.                                                            )
  91.                                                            (setq p0 (trans pt 0 nrm))
  92.                                                            (<= 0 (* (sin (angle p0 p-)) (sin (angle p0 p+)))) ;; LM Mod
  93.                                                       )
  94.                                                     )
  95.                                           ) lst
  96.                             )
  97.                     ) 2
  98.                )
  99.           )
  100.         )
  101.       )
  102.     )
  103.   )
  104.  
  105.   (defun nn_processpla ( pla / ipl pla1 pla2 )
  106.     (setq pla1 pla)
  107.     (foreach p1 pla1
  108.       (setq pla2 (vl-remove p1 pla1))
  109.       (foreach p2 pla2
  110.         (setq ipl (cons (inters (car p1) (polar (car p1) (cadr p1) 1.0) (car p2) (polar (car p2) (cadr p2) 1.0) nil) ipl))
  111.       )
  112.     )
  113.     (setq ipl (vl-remove-if-not (function (lambda ( p ) (nn_insidep p lw))) (vl-remove nil ipl)))
  114.   )
  115.  
  116.   (defun nn_distp2t ( p tt )
  117.     (distance p (inters p (polar p (+ (angle (car tt) (cadr tt)) (* 0.5 pi)) 1.0) (car tt) (cadr tt) nil))
  118.   )
  119.  
  120.   (defun nn_removesingles ( l / a )
  121.     (while (setq a (vl-some (function (lambda ( x ) (if (= (1- (length l)) (length (vl-remove-if (function (lambda ( y ) (equal x y 1e-6))) l))) x))) l))
  122.       (setq l (vl-remove a l))
  123.     )
  124.     l
  125.   )
  126.  
  127.   (defun nn_findipinterschilds ( ip pla / pla1 pla2 r )
  128.     (setq pla1 pla)
  129.     (foreach p1 pla1
  130.       (setq pla2 (vl-remove p1 pla1))
  131.       (foreach p2 pla2
  132.         (if (equal ip (inters (car p1) (polar (car p1) (cadr p1) 1.0) (car p2) (polar (car p2) (cadr p2) 1.0) nil) 1e-6)
  133.           (setq r (list p1 p2))
  134.         )
  135.       )
  136.     )
  137.     r
  138.   )
  139.  
  140.   (defun nn_processtxtipl ( tl / ipl tl1 tl2 )
  141.     (setq tl1 tl)
  142.     (foreach t1 tl1
  143.       (setq tl2 (vl-remove t1 tl1))
  144.       (foreach t2 tl2
  145.         (setq ipl (cons (inters (car t1) (polar (car t1) (angle (car t1) (cadr t1)) 1.0) (car t2) (polar (car t2) (angle (car t2) (cadr t2)) 1.0) nil) ipl))
  146.       )
  147.     )
  148.     (setq ipl (vl-remove nil ipl))
  149.   )
  150.  
  151.   (defun nn_prefetch nil
  152.     (if ip
  153.       (setq p1p2 (nn_findipinterschilds ip pla))
  154.       (setq p1p2 nil)
  155.     )
  156.     (setq f nil)
  157.     (if p1p2
  158.       (progn
  159.         (setq tt (vl-some (function (lambda ( x ) (if (and (assoc (car x) p1p2) (assoc (cadr x) p1p2)) (progn (setq f t) x)))) tlo))
  160.         (if (or (vl-some (function (lambda ( x ) (equal (distance ip (car (car p1p2))) (+ (distance ip x) (distance x (car (car p1p2)))) 1e-6))) (vl-remove-if (function (lambda ( x ) (equal x (car (car p1p2)) 1e-6))) (vl-remove-if (function (lambda ( x ) (equal x ip 1e-6))) (mapcar (function car) ipldtl)))) (vl-some (function (lambda ( x ) (equal (distance ip (car (cadr p1p2))) (+ (distance ip x) (distance x (car (cadr p1p2)))) 1e-6))) (vl-remove-if (function (lambda ( x ) (equal x (car (cadr p1p2)) 1e-6))) (vl-remove-if (function (lambda ( x ) (equal x ip 1e-6))) (mapcar (function car) ipldtl)))))
  161.           (setq f nil)
  162.         )
  163.       )
  164.     )
  165.   )
  166.  
  167.   (defun nn_positionvl ( e l tol / nn_car_vlmember_if )
  168.     (defun nn_car_vlmember_if ( f l / ff r )
  169.       (setq ff (function (lambda ( x ) (if (apply f (list x)) (setq r x)))))
  170.       (vl-some ff l)
  171.       r
  172.     )
  173.     (vl-position (nn_car_vlmember_if (function (lambda ( x ) (equal e x tol))) l) l)
  174.   )
  175.  
  176.   (defun nn_carsort ( lst cmp / rtn )
  177.     (setq rtn (car lst))
  178.     (foreach itm (cdr lst)
  179.       (if (apply cmp (list itm rtn))
  180.         (setq rtn itm)
  181.       )
  182.     )
  183.     rtn
  184.   )
  185.  
  186.   (defun nn_car_vlmember_if ( f l / ff r )
  187.     (setq ff (function (lambda ( x ) (if (apply f (list x)) (setq r x)))))
  188.     (vl-some ff l)
  189.     r
  190.   )
  191.  
  192.  
  193. ;;; main 2droof ;;;
  194.  
  195.   (setq lw (car (entsel "\nPick a closed polygon : ")))
  196.   (setq ti (car (_vl-times)))
  197.   (setq n (cdr (assoc 90 (entget lw))))
  198.   (setq pl (mapcar (function cdr) (vl-remove-if (function (lambda ( x ) (/= (car x) 10))) (entget lw))))
  199.   (setq tl (mapcar (function (lambda ( a b ) (list a b))) pl (append (cdr pl) (list (car pl)))))
  200.   (setq tlo tl)
  201.   (setq pla (mapcar (function (lambda ( p mp ) (list p (angle p mp)))) pl (mapcar (function (lambda ( a b ) (if (nn_clockwisep (car a) (car b) (cadr b)) (nn_mid (polar (cadr a) (angle (cadr a) (car a)) -1.0) (polar (car b) (angle (car b) (cadr b)) -1.0)) (nn_mid (polar (cadr a) (angle (cadr a) (car a)) 1.0) (polar (car b) (angle (car b) (cadr b)) 1.0))))) (cons (last tl) tl) tl)))
  202.   (setq plaa pla)
  203.   (while (> n 0)
  204.     (if (null ipll)
  205.       (setq ipl (nn_unique (nn_processpla plaa)))
  206.       (setq ipl (nn_unique (nn_processpla (nn_unique pla))))
  207.     )
  208.     (if (equal ipl iplo 1e-6)
  209.       (setq n 0)
  210.     )
  211.     (if fff
  212.       (progn
  213.         (setq pl (mapcar (function car) pla))
  214.         (foreach x pl
  215.           (setq lil (cons (list x ip) lil))
  216.         )
  217.         (setq n 0)
  218.       )
  219.       (progn
  220.         (setq ipldtl (mapcar (function (lambda ( p ) (list p (vl-sort (mapcar (function (lambda ( tt ) (nn_distp2t p tt))) tlo) (function <))))) ipl))
  221.         (setq ipldtl (mapcar (function (lambda ( x ) (list (car x) (nn_removesingles (cadr x))))) ipldtl))
  222.         (setq ipldtl (vl-remove-if (function (lambda ( x ) (< (- (length (cadr x)) (length (vl-remove-if (function (lambda ( y ) (equal y (car (cadr x)) 1e-6))) (cadr x)))) 3))) ipldtl))
  223.         (if ipll
  224.           (setq ipldtl (vl-remove-if (function (lambda ( x ) (nn_positionvl (car x) ipll 1e-6))) ipldtl))
  225.         )
  226.         (if (null ipll)
  227.           (setq ipldtlo ipldtl)
  228.         )
  229.         (setq ip (car (nn_carsort (nn_unique (append ipldtl ipldtlo)) (function (lambda ( a b ) (< (car (cadr a)) (car (cadr b))))))))
  230.         (setq ipldtlo (vl-remove-if (function (lambda ( x ) (equal (car x) ip 1e-6))) ipldtlo))
  231.         (setq ipp nil)
  232.         (if (progn (nn_prefetch) (not f))
  233.           (if ipll
  234.             (progn
  235.               (foreach y (reverse pal)
  236.                 (setq ipp (cons (vl-remove-if-not (function (lambda ( x ) (vl-some (function (lambda ( a ) (or (equal (rem (+ a pi) (* 2 pi)) (angle (car y) x) 1e-6) (equal a (angle (car y) x) 1e-6)))) (cadr y)))) (mapcar (function car) (vl-sort ipldtl (function (lambda ( a b ) (> (car (cadr a)) (car (cadr b)))))))) ipp))
  237.               )
  238.               (setq ipp (apply (function append) ipp))
  239.               (if p1p2
  240.                 (setq ipp (cons ip ipp))
  241.               )
  242.               (setq ipp (nn_unique ipp))
  243.               (setq ipp (mapcar (function (lambda ( p ) (list p (nn_findipinterschilds p pla)))) ipp))
  244.               (setq ipp (vl-remove-if (function (lambda ( x ) (null (cadr x)))) ipp))
  245.               (setq ipp (vl-remove-if (function (lambda ( x ) (or (vl-some (function (lambda ( y ) (equal (distance (car x) (car (car (cadr x)))) (+ (distance (car x) y) (distance y (car (car (cadr x))))) 1e-6))) (vl-remove-if (function (lambda ( z ) (equal z (car (car (cadr x))) 1e-6))) (vl-remove-if (function (lambda ( z ) (equal z (car x) 1e-6))) (mapcar (function car) ipldtl)))) (vl-some (function (lambda ( y ) (equal (distance (car x) (car (cadr (cadr x)))) (+ (distance (car x) y) (distance y (car (cadr (cadr x))))) 1e-6))) (vl-remove-if (function (lambda ( z ) (equal z (car (cadr (cadr x))) 1e-6))) (vl-remove-if (function (lambda ( z ) (equal z (car x) 1e-6))) (mapcar (function car) ipldtl)))) (vl-some (function (lambda ( y / ii ) (and (setq ii (inters (car x) (car (car (cadr x))) (car y) (cadr y) t)) (not (equal ii (car x) 1e-6)) (not (equal ii (car (car (cadr x))) 1e-6)) (not (equal ii (car y) 1e-6)) (not (equal ii (cadr y) 1e-6))))) (append tlo lil)) (vl-some (function (lambda ( y / ii ) (and (setq ii (inters (car x) (car (cadr (cadr x))) (car y) (cadr y) t)) (not (equal ii (car x) 1e-6)) (not (equal ii (car (cadr (cadr x))) 1e-6)) (not (equal ii (car y) 1e-6)) (not (equal ii (cadr y) 1e-6))))) (append tlo lil))))) ipp))
  246.               (setq dl (mapcar (function (lambda ( y ) (car (cadr (nn_car_vlmember_if (function (lambda ( x ) (equal (car x) y 1e-6))) ipldtl))))) (mapcar (function car) ipp)))
  247.               (if dl
  248.                 (setq ip (car (nth (vl-position (nn_carsort dl (function <)) dl) ipp)))
  249.               )
  250.             )
  251.           )
  252.         )
  253.         (if (vl-every (function (lambda ( x ) (equal ip (inters (car (car pla)) (polar (car (car pla)) (cadr (car pla)) 1.0) (car x) (polar (car x) (cadr x) 1.0) nil) 1e-6))) (cdr pla))
  254.           (setq fff t)
  255.         )
  256.         (setq d (car (cadr (nn_car_vlmember_if (function (lambda ( x ) (equal (car x) ip 1e-6))) (nn_unique (append ipldtl ipldtlo))))))
  257.         (setq al nil a nil)
  258.         (nn_prefetch)
  259.         (if (null p1p2)
  260.           (setq ip nil)
  261.         )
  262.         (if (and tt f)
  263.           (progn
  264.             (setq tl (vl-remove tt tl))
  265.             (setq n (1- n))
  266.           )
  267.         )
  268.         (if f
  269.           (progn
  270.             (setq t1 (vl-some (function (lambda ( x ) (if (or (assoc (car x) (list (car p1p2))) (assoc (cadr x) (list (car p1p2)))) x))) tl))
  271.             (setq t2 (vl-some (function (lambda ( x ) (if (or (assoc (car x) (list (cadr p1p2))) (assoc (cadr x) (list (cadr p1p2)))) x))) tl))
  272.           )
  273.           (setq t1 nil t2 nil)
  274.         )
  275.         (if ip
  276.           (setq ipll (cons ip ipll))
  277.         )
  278.         (if (and t1 t2 ip)
  279.           (progn
  280.             (setq a (angle (inters (car t1) (cadr t1) (car t2) (cadr t2) nil) ip))
  281.             (if a
  282.               (progn
  283.                 (setq pla (cons (list ip a) pla))
  284.                 (setq pla (vl-remove (car p1p2) pla) pla (vl-remove (cadr p1p2) pla))
  285.                 (setq lil (cons (list (car (car p1p2)) ip) lil))
  286.                 (setq lil (cons (list (car (cadr p1p2)) ip) lil))
  287.                 (setq pal (cons (list ip (list a)) pal))
  288.               )
  289.             )
  290.           )
  291.           (if (and ip (null fff))
  292.             (progn
  293.               (setq lil (cons (list (car (car p1p2)) ip) lil))
  294.               (setq lil (cons (list (car (cadr p1p2)) ip) lil))
  295.               (if (setq p (vl-some (function (lambda ( x ) (if (vl-position x plaa) x))) p1p2))
  296.                 (progn
  297.                   (setq n (1- n))
  298.                   (setq tll (vl-remove-if-not (function (lambda ( x ) (vl-some (function (lambda ( y ) (nn_positionvl p y 1e-6))) x))) tlo))
  299.                   (setq tll (car (vl-remove-if-not (function (lambda ( x ) (and (nn_positionvl (car x) (vl-remove nil (mapcar (function car) lil)) 1e-6) (nn_positionvl (cadr x) (vl-remove nil (mapcar (function car) lil)) 1e-6)))) tll)))
  300.                   (setq tl (vl-remove tll tl))
  301.                 )
  302.               )
  303.               (setq tll (vl-remove-if-not (function (lambda ( x ) (equal d (nn_distp2t ip x) 1e-6))) tl))
  304.               (setq txtipl (nn_processtxtipl tll))
  305.               (setq txtipl (vl-remove-if (function (lambda ( x ) (nn_positionvl x pl 1e-6))) txtipl))
  306.               (setq al (mapcar (function (lambda ( p ) (angle p ip))) txtipl))
  307.               (setq al (vl-remove-if (function (lambda ( x ) (nn_positionvl x (mapcar (function cadr) p1p2) 1e-6))) al))
  308.               (setq al (nn_unique al))
  309.               (if al
  310.                 (progn
  311.                   (setq pla (append pla (mapcar (function (lambda ( a ) (list ip a))) al)))
  312.                   (setq pal (cons (list ip al) pal))
  313.                 )
  314.               )
  315.               (setq pla (vl-remove (car p1p2) pla) pla (vl-remove (cadr p1p2) pla))
  316.             )
  317.           )
  318.         )
  319.       )
  320.     )
  321.     (setq iplo ipl)
  322.   )
  323.   (setq lil (vl-remove-if (function (lambda ( x ) (equal (car x) (cadr x) 1e-6))) lil))
  324.   (setq lil (nn_unique lil))
  325.   (setq lil (vl-remove-if (function (lambda ( x ) (vl-some (function (lambda ( y ) (null y))) x))) lil))
  326.   (setq lipl (apply (function append) lil))
  327.   (setq lipl (nn_unique (vl-remove-if (function (lambda ( x ) (or (= (length (vl-remove x lipl)) (1- (length lipl))) (= (length (vl-remove x lipl)) (- (length lipl) 3))))) lipl)))
  328.   (if lipl
  329.     (setq lil (cons (list (car lipl) (cadr lipl)) lil))
  330.   )
  331.   (foreach li lil
  332.     (entmake (list '(0 . "LINE") (cons 10 (car li)) (cons 11 (cadr li))))
  333.   )
  334.   (prompt "\nElapsed time : ") (princ (rtos (- (car (_vl-times)) ti) 2 20)) (prompt " milliseconds...")
  335.   (princ)
  336. )
  337.  
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 08, 2021, 02:13:45 PM
@d2010, I've revised my last code, so perhaps you'll have to revise your too...
Sorry for inconvenience...
M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: d2010 on January 08, 2021, 05:38:49 PM
In my BrisCad2020 x86 , your-program failed to work.
 :reallysad:
@d2010, I've revised my last code, so perhaps you'll have to revise your too...
Sorry for inconvenience...
M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ronjonp on January 08, 2021, 11:51:59 PM
In my BrisCad2020 x86 , your-program failed to work.
 :reallysad:
@d2010, I've revised my last code, so perhaps you'll have to revise your too...
Sorry for inconvenience...
M.R.
Your post is very strange.  :?
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 09, 2021, 12:50:33 AM
My BricsCAD did ultimate roof at approx. 13 min...

Here is picture :

(https://serving.photos.photobox.com/92308774fc2a0b34329e0e144e9e31f48d946c5f0172bc75a13a65e62c306ec241e922c8.jpg)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 11, 2021, 01:53:17 PM
On this example, my code is wrong and 2droof-final.lsp from zip I posted finds solution (ch_lhjd's code) - though after iterations of checking...

:(
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 13, 2021, 05:03:49 AM
I've debugged last posted DWG example and this is version I use now...

M.R.

[EDIT : LSP file removed due to lack of interest for download...]
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 13, 2021, 05:08:50 AM
Still there is another example, where AutoCAD crashed from normal mode - unhandeled exception occurred (CX000, bla, bla, bla,...) and from VLIDE - debugging mode, it passed... I wonder is it my PC machines (very low hardware performances, low RAM, or something...) From BricsCAD it worked well...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: BIGAL on January 13, 2021, 07:54:23 PM
I tried it on Bricscad V19 works great maybe 1 suggestion use acet-progress-bar function so looks like something happening maybe use the vertice number.

So would have like 1-44. Just did 10 vertice it was instant a more typical shape, so would not be needed maybe around 20+ vertice.

Code: [Select]
;; EXAMPLE LISP SHORT VERSION OF PROGRESS-BAR

(defun progressbar (a b c d)
(cond ((= 0 a)
(acet-ui-progress-init d 100)
(setq c 0)
)
((= 1 a)
(setq c (+ (/ 100.0 b) c))
(acet-ui-progress-safe c)
)
((= 2 a)
(acet-ui-progress-done)
)
)
c
)


(setq repeatvariable 45) ; 45 just a sample number
(setq progress (Progressbar 0 repeatvariable progress "Processing:")) ;Create the progress bar, total length is repeatvariable or 10000
(repeat repeatvariable
(setq progress (Progressbar 1 repeatvariable progress ""))
(alert "press") ; code goes here
)
(Progressbar 2 0 0 "") ;arg(a) is set to 2 to close the progress bar, arg (b), (c) & (d) can be blanks
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 13, 2021, 11:33:15 PM
@BIGAL, thanks for suggestion and review...
I have to inform you that I had typo on one place - should be tree1 and not tree2, and plus, it should be (vl-remove-if-not ... ) and not (vl-remove-if ... )... I reattached corrected version. IMHO it's now good, but you'll never know - I mod. it at least 10 times and it was always something stupid...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 14, 2021, 04:14:57 AM
HA, it was just a moment while working on routine... My latest version is finished... tricky roof-passed test in AutoCAD...
With my latest intervention it is also considered LWPOLYLINE that was made from closed MLINE... Previously it didn't worked with those shapes... So everything is working well... If someone notice something, please inform me...
Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 14, 2021, 05:06:04 AM
It's me again... This one it can't do... ch_lhjd's version is relatively good - better than mine, but I won't debug my code further... I am tired, I leave that to someone fresh...
Bye, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 14, 2021, 02:51:50 PM
You know what... I'll tell you the secret to gain speed...

Quote
  (defun LM:Inside-p (pt ent / unit v^v _GroupByNum fd1 fd2 par lst nrm obj tmp)
    (vl-load-com)
    (defun unit (v / d)
      (if (not (equal (setq d (distance '(0.0 0.0 0.0) v)) 0.0 1e-8))
        (mapcar (function (lambda (x) (/ x d))) v)
      )
    )
    (defun v^v (u v)
      (list
        (- (* (cadr u) (caddr v)) (* (caddr u) (cadr v)))
        (- (* (caddr u) (car v)) (* (car u) (caddr v)))
        (- (* (car u) (cadr v)) (* (cadr u) (car v)))
      )
    )
    (defun _GroupByNum (l n / r)
      (if l
        (cons (reverse (repeat n (setq r (cons (car l) r) l (cdr l)) r)) (_GroupByNum l n))
      )
    )
    (if (= (type ent) 'VLA-OBJECT)
      (setq obj ent ent (vlax-vla-object->ename ent))
      (setq obj (vlax-ename->vla-object ent))
    )
    (if (vlax-curve-isplanar ent)
      (progn
        ;(setq fd1 (vlax-curve-getfirstderiv ent (setq par (vlax-curve-getstartparam ent))))
        ;(while (or (equal fd1 (setq fd2 (vlax-curve-getfirstderiv ent (setq par (+ par 0.001)))) 1e-3) (null (setq nrm (unit (v^v fd1 fd2))))))
        (setq nrm '(0.0 0.0 1.0)) ;;; mod by M.R. - gaining speed
        (setq lst
          (_GroupByNum
            (vlax-invoke
              (setq tmp
                (vlax-ename->vla-object
                  (entmakex
                    (list
                      (cons 0 "RAY")
                      (cons 100 "AcDbEntity")
                      (cons 100 "AcDbRay")
                      (cons 10 pt)
                      (cons 11 (trans '(1.0 0.0 0.0) nrm 0))
                    )
                  )
                )
              )
              'IntersectWith obj acextendnone
            ) 3
          )
        )
        (vla-delete tmp)
        ;; gile:
        (and
          lst
          (not (vlax-curve-getparamatpoint ent pt))
          (= 1 (rem (length (vl-remove-if (function (lambda (p / pa p- p+ p0)
                                                      (setq pa (vlax-curve-getparamatpoint ent p))
                                                      (and (setq p- (cond ((setq p- (vlax-curve-getPointatParam ent (- pa 1e-8)))
                                                                           (trans p- 0 nrm)
                                                                          )
                                                                          ((trans (vlax-curve-getPointatParam ent (- (vlax-curve-getEndParam ent) 1e-8)) 0 nrm)
                                                                          )
                                                                    )
                                                           )
                                                           (setq p+ (cond ((setq p+ (vlax-curve-getPointatParam ent (+ pa 1e-8)))
                                                                           (trans p+ 0 nrm)
                                                                          )
                                                                          ((trans (vlax-curve-getPointatParam ent (+ (vlax-curve-getStartParam ent) 1e-8)) 0 nrm)
                                                                          )
                                                                    )
                                                           )
                                                           (setq p0 (trans pt 0 nrm))
                                                           (<= 0 (* (sin (angle p0 p-)) (sin (angle p0 p+)))) ;; LM Mod
                                                      )
                                                    )
                                          ) lst
                            )
                    ) 2
               )
          )
        )
      )
    )
  )
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: d2010 on January 15, 2021, 04:41:27 AM
Nice Job!
Thank you pacman.z80 , paperboy.z80, or postaman.z80
You code-lisp is fantastic, but is huge./ or too complex , for manage and search bugs.
I  fill the source/rinarm.lsp with "z80Spectrum -games-names"
(defun c:2droof ( / nn_unique nn_mid nn_clockwisep nn_insidep nn_processpla
Code - Auto/Visual Lisp: [Select]
  1. /*c2s:
  2.          lw=car(entsel("\nPick a closed polygonal LWPOLYLINE..."));
  3.          sos_z80=read("/");
  4.          if (lw)
  5.            { ti=car(_vl_times());
  6.            gc();
  7.           n=cdr(assoc(90,entget(lw)));
  8.           pl=mapcar(function(cdr),vl.remove_if(function(lambda(xeno_z80(), (car(xeno_z80) != 10))),entget(lw)));
  9.           tl=mapcar(function(lambda(arkanoid_z80(bomberman_x80),lISt(arkanoid_z80,bomberman_x80))),pl,append(cdr(pl),lISt(car(pl))));
  10.           tlo=tl,
  11.           utlo=unioncollinearplaneprints(tlo),
  12.           pla=mapcar(function(lambda(pulsoid_z80(mp),lISt(pulsoid_z80,angle(pulsoid_z80,mp)))),pl,mapcar(function(lambda(arkanods_z80(bomberman_z80),((clockwise_p(car(arkanods_z80),car(bomberman_z80),cadr(bomberman_z80)))?mid(polar(cadr(arkanods_z80),angle(cadr(arkanods_z80),car(arkanods_z80)),-1.0),polar(car(bomberman_z80),angle(car(bomberman_z80),cadr(bomberman_z80)),-1.0)):mid(polar(cadr(arkanods_z80),angle(cadr(arkanods_z80),car(arkanods_z80)),1.0),polar(car(bomberman_z80),angle(car(bomberman_z80),cadr(bomberman_z80)),1.0))))),cons(last(tl),tl),tl));
  13.           plaa=pla
  14.           while(n > 0)
  15.             { if (null(ipll)) ipl=unique(processpla(plaa))
  16.                  else  ipl=unique(processpla(unique(pla)));
  17.  
  18.            if (equal(ipl,iplo,1e-6)) n=0;
  19.          if (fff) { pl=mapcar(function(car),pla);
  20.                      foreach(x,pl){lil=cons(lISt(x,ip),lil);}
  21.                    n=0;
  22.                  };
  23.     else
  24.            { ipo=ip;
  25.              ipldtl=mapcar(function(lambda(postman_z80(),lISt(postman_z80,vl.sort(mapcar(function(lambda(tt(),distp2t(postman_z80,tt))),utlo),function(read("<")))))),ipl);
  26.              ipldtl=mapcar(function(lambda(xeno_z80(),lISt(car(xeno_z80),removedoubles(removesingles(cadr(xeno_z80)))))),ipldtl);
  27.              ipldtl=vl.remove_if(function(lambda(xeno(),null(cadr(xeno)))),ipldtl);
  28.              ipldtl=(ipll)?vl.remove_if(function(lambda(xarax_z80(),_vl_position(car(xarax),ipll,1e-6))),ipldtl):ipldtl;
  29.              ipldtl=mapcar(function(lambda(xtro_z80(),lISt(car(xtro),vl.sort(cadr(xtro_z80),function(read("<")))))),ipldtl);
  30.              ipldtlo=vl.sort(ipldtl,function(lambda(arkanoid_z80(b), (car(cadr(arkanoid_z80)) <car(cadr(b))))));
  31.              ipldtlo=vl.remove_if(function(lambda(xonix_z80(),_vl_position(car(xonix_z80),ipll,1e-6))),ipldtlo);
  32.              ip=car(car(ipldtlo))
  33.              while((!process()  && ipldtlo)) {ipldtlo=cdr(ipldtlo);ip=car(car(ipldtlo))};
  34.              ipp=nil;
  35.              if (ipll) { foreach(y,reverse(pal)){ipp=cons(vl.remove_if_not(function(lambda(xorem_z80(),vl.some(function(lambda(a(),#TrimR() (equal(rem( (a+kpi), (2*kpi)),angle(car(y),xorem_z80),1e-6) ||equal(a,angle(car(y),xorem_z80),1e-6)))),cadr(y)))),mapcar(function(car),vl.remove_if(function(lambda(xorem_z80(),_vl_position(car(xorem_z80),ipll,1e-6))),ipldtl))),ipp);};
  36.                  ipp=apply(function(append),ipp);
  37.              if (ip  && p1p2) ipp=cons(ip,ipp);
  38.              ipp=unique(ipp);
  39.              ipp=mapcar(function(lambda(paperboy_z80(),lISt(paperboy_z80,findipinterschilds(paperboy_z80,pla)))),ipp);
  40.              ipp=vl.remove_if(function(lambda(xevious_z80(),null(cadr(xevious_z80)))),ipp);
  41.              ipp=vl.remove_if(function(lambda(xen_z80(),(vl.some(function(lambda(yeti_z80(),equal(distance(car(xen_z80),car(car(cadr(xen_z80)))), (distance(car(xen_z80),y)+distance(y,car(car(cadr(xen_z80))))),1e-6))),vl.remove_if(function(lambda(z(),equal(z,car(car(cadr(xen_z80))),1e-6))),vl.remove_if(function(lambda(z(),equal(z,car(xen_z80),1e-6))),mapcar(function(car),ipldtl)))) || vl.some(function(lambda(yomp_z80(),equal(distance(car(xen_z80),car(cadr(cadr(xen_z80)))), (distance(car(xen_z80),yomp_z80)+distance(yomp_z80,car(cadr(cadr(xen_z80))))),1e-6))),vl.remove_if(function(lambda(z(),equal(z,car(cadr(cadr(x))),1e-6))),vl.remove_if(function(lambda(z(),equal(z,car(x),1e-6))),mapcar(function(car),ipldtl))))#TrimR() ||       vl.some(function(lambda(yocan_z80(sos_z80,ii),#TrimR() (ii=inters(car(xen_z80),car(car(cadr(xen_z80))),car(yocan_z80),cadr(yocan_z80),t)  &&!equal(ii,car(xen_z80),1e-6)#TrimR() &&       !equal(ii,car(car(cadr(xen_z80))),1e-6)#TrimR() &&       !equal(ii,car(yocan_z80),1e-6)#TrimR() &&       !equal(ii,cadr(yocan_z80),1e-6)))),append(tlo,lil))#TrimR() ||       vl.some(function(lambda(yankee1_z80(sos_z80,ii),#TrimR() (ii=inters(car(xen_z80),car(cadr(cadr(xen_z80))),car(yankee1_z80),cadr(yankee1_z80),t)  &&!equal(ii,car(xen_z80),1e-6)#TrimR() &&       !equal(ii,car(cadr(cadr(xen_z80))),1e-6)#TrimR() &&       !equal(ii,car(yankee1_z80),1e-6)#TrimR() && (! equal(ii,cadr(yankee1_z80),1e-6))))),append(tlo,lil))))),ipp);
  42.              dl=mapcar(function(lambda(yogibear_z80(),car(cadr(car_vl.member_if(function(lambda(xeno_z80(),equal(car(xeno_z80),yogibear_z80,1e-6))),ipldtl))))),mapcar(function(car),ipp));
  43.              ip=(dl)?car(nth(vl.position(car_sort(dl,function(read("<"))),dl),ipp)):ip;
  44.          }
  45.     if (vl.every(function(lambda(xmaze_z80(),equal(ip,inters(car(car(pla)),polar(car(car(pla)),cadr(car(pla)),1.0),car(xmaze_z80),polar(car(xmaze_z80),cadr(xmaze_z80),1.0),nil),1e-6))),cdr(pla)) )
  46.          >fff=t;
  47.        process()
  48.       itt=vl.some(function(lambda(xenon_z80(), if ((equal(distance(ip,car(car(p1p2))), (distance(ip,car(xenon_z80))+distance(car(xenon_z80),car(car(p1p2)))),1e-6) ||equal(distance(ip,car(cadr(p1p2))), (distance(ip,car(x))+distance(car(xenon_z80),car(cadr(p1p2)))),1e-6)) )
  49.              car(xenon_z80),
  50.              vl.remove_if(function(lambda(xenon_z80(),equal(ip,car(xenon_z80),1e-6))),ipp)),
  51.      if (p1p2  && itt)) { ip=itt;process(); };
  52.      if (equal(ipo,ip,1e-6)) n=0;
  53.      d=car(cadr(car_vl.member_if(function(lambda(xonix_z80(),equal(car(xonix_z80),ip,1e-6))),ipldtl)));
  54.      al=nil,a=nil
  55.      if (tt)  && f) tl=vl.remove(tt,tl);
  56.      if (f) { t1=vl.some(function(lambda(xadom_z80(),
  57.               (assoc(car(xadom_z80),lISt(car(p1p2))) || assoc(cadr(xadom_z80),lISt(car(p1p2)))) )?
  58.                xadom_z80:tl);
  59.                t2=vl.some(function(lambda(xtrzth_z80(),(assoc(car(xtrzth_z80),lISt(cadr(p1p2))) ||assoc(cadr(xtrzth_z80),lISt(cadr(p1p2)))) )?xtrzth_z80:tl);
  60.              };
  61.     else
  62.     t1=nil,
  63.     t2=nil;
  64.     if (ip ) ipll=cons(ip,ipll);
  65.     if (t1  && t2#TrimR() &&ip) )
  66.          { if (inters(car(t1),cadr(t1),car(t2),cadr(t2),nil) )
  67.              a=angle(inters(car(t1),cadr(t1),car(t2),cadr(t2),nil),ip);
  68.     else
  69.            a=angle(car(t1),cadr(t1));
  70.          };
  71.    if (a) { pla=cons(lISt(ip,a),pla);
  72.             pla=vl.remove_if(function(lambda(xarax_z80(),equal(car(xarax_z80),car(car(p1p2)),1e-6))),pla),pla=vl.remove_if(function(lambda(xarax_z80(),equal(car(xarax_z80),car(cadr(p1p2)),1e-6))),pla);
  73.             if (car(car(p1p2))) lil=cons(lISt(car(car(p1p2)),ip),lil);
  74.             if (car(cadr(p1p2)) lil=cons(lISt(car(cadr(p1p2)),ip),lil);
  75.             pal=cons(lISt(ip,a)),pal)
  76.            };
  77.            };
  78.     else if (ip  && null(fff))
  79.            { if (car(car(p1p2)) ) lil=cons(lISt(car(car(p1p2)),ip),lil);
  80.              if (car(cadr(p1p2))) lil=cons(lISt(car(cadr(p1p2)),ip),lil);
  81.              p=vl.some(function(lambda(xanthius_z80(),(vl.position(xanthius_z80,plaa))?xanthius_z80:p1p2);
  82.              if (p) { tll=vl.remove_if_not(function(lambda(xarq_z80(),vl.some(function(lambda(y(),_vl_position(p,y,1e-6))),xarq_z80))),tlo);
  83.                       tll=car(vl.remove_if_not(function(lambda(xadom_z80(),#TrimR() (_vl_position(car(xadom_z80),vl.remove(nil,mapcar(function(car),lil)),1e-6)  &&_vl_position(cadr(xadom_z80),vl.remove(nil,mapcar(function(car),lil)),1e-6)))),tll));
  84.                       tl=vl.remove(tll,tl);
  85.                     };
  86.              tll=vl.remove_if_not(function(lambda(xcel_z80(),equal(d,distp2t(ip,xcel_z80),1e-6))),tl);
  87.              txtipl=processtxtipl(tll);
  88.              txtipl=vl.remove_if(function(lambda(xarax_x80(),_vl_position(xarax_x80,pl,1e-6))),txtipl);
  89.              al=mapcar(function(lambda(pacman_z80(),((listp(pacman_z80))?angle(pacman_z80,ip):pacman_z80))),txtipl);
  90.              al=vl.remove_if(function(lambda(xecutor_z80(),#TrimR() (_vl_position(xecutor_z80,mapcar(function(cadr),p1p2),1e-6) ||_vl_position(rem( (xecutor_z80+kpi), (2*kpi)),mapcar(function(cadr),p1p2),1e-6)))),al);
  91.              al=unique(al)
  92.              if (al)
  93.                    { pla=append(mapcar(function(lambda(astroball_z80(),lISt(ip,astroball_z80))),al),pla);
  94.                       pal=cons(lISt(ip,al),pal);
  95.                     };
  96.              pla=vl.remove_if(function(lambda(xeno_z80(),equal(car(xeno_z80),car(car(p1p2)),1e-6))),pla),pla=vl.remove_if(function(lambda(xeno_z80(),equal(car(xeno_z80),car(cadr(p1p2)),1e-6))),pla)
  97.           };
  98.   };
  99.  iplo=ipl;
  100.   };
  101.    lil=vl.remove_if(function(lambda(xenophob_z80(),equal(car(xenophob_z80),cadr(xenophob_z80),1e-6))),lil);
  102.    lil=unique(lil);
  103.    lil=vl.remove_if(function(lambda(xmaze_z80(),vl.some(function(lambda(y(),null(y))),xmaze_z80))),lil);
  104.    lipl=apply(function(append),lil);
  105.    lipl=unique(vl.remove_if(function(lambda(xmas_lud_z80(),#TrimR() ( (length(vl.remove(xmas_lud_z80,lipl)) == (length(lipl)-1)) ||  (length(vl.remove(xmas_lud_z80,lipl)) == (length(lipl)-3))) )),lipl));
  106.    lipl=vl.remove_if(function(lambda(xenoii_z80(),vl.some(function(lambda(li(),#TrimR() (equal(distance(car(li),cadr(li)), (distance(car(li),xenoii_z80)+distance(xenoii_z80,cadr(li))),1e-6)  &&!equal(xenoii_z80,car(li),1e-6)#TrimR() &&       !equal(xenoii_z80,cadr(li),1e-6)))),lil))),lipl);
  107.    lil= (lipl )? cons(lISt(car(lipl),cadr(lipl)),lil):lil;
  108.    lil=vl.remove_if(function(lambda(xavior_z80(),#TrimR() (null(car(xavior_z80)) ||null(cadr(xavior_z80))) )),lil);
  109.    foreach(li,lil){entmake(lISt(cons(0,"LINE"),cons(10,car(li)),cons(11,cadr(li))));
  110.   };
  111.   gc();
  112.   prompt("\nElapsed time : ");
  113.   princ(rtos( (car(_vl_times())-ti),2,20));
  114.   prompt(" milliseconds...");
  115.  };
  116. */
  117.  



%include=arkanods_z80
%include=arkanoid_z80
%include=astroball_z80
%include=mid
%include=pacman_z80
%include=paperboy_z80
%include=postman_z80
%include=pulsoid_z80
%include=xadom_z80
%include=xanthius_z80
%include=xarax_x80
%include=xarax_z80
%include=xarq_z80
%include=xavior_z80
%include=xcel_z80
%include=xecutor_z80
%include=xen_z80
%include=xeno
%include=xeno_z80
%include=xenoii_z80
%include=xenon_z80
%include=xenophob_z80
%include=xevious_z80
%include=xmas_lud_z80
%include=xmaze_z80
%include=xonix_z80
%include=xorem_z80
%include=xtro_z80
%include=xtrzth_z80
%include=yankee1_z80
%include=yeti_z80
%include=yocan_z80
%include=yogibear_z80
%include=yomp_z80

Do you like this myLock of your-lisp?
https://youtu.be/i_FlinBI6ck?t=1435 (https://youtu.be/i_FlinBI6ck?t=1435)

Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 15, 2021, 05:26:16 AM
You quoted text that I haven't wrote... You spelled my name wrongly... You are posting gibberish code examples... If you say that my code is not good and helpful, then show us your version in ALISP format - real *.lsp... I am not saying that my version is the best, I showed examples where it won't give results, but I also showed solution for ultimate roof and solution based on my algorithm that differs from built-in offset algorithm... So if you have something useful, we'll be grateful to see it, otherwise I must say that you are spamming this topic that is IMHO very profound and well composed...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: Coder on January 15, 2021, 06:58:51 AM
You are posting gibberish code examples...
.... I must say that you are spamming this topic that is IMHO very profound and well composed...

I am completely agree with you and all of his posts are real rubbish and has nothing to do with any thread he contributed with since his first post then that leads me to say that he is really a spammer definitely.

Great work by the way Marko.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: d2010 on January 15, 2021, 08:20:44 AM
AProgramul tau Lisp este de bun, dar daca tu cresti complexitatea ribarm2droof.lsp, atunci,  in viitor, programul tau nu va avea viitor, deoarece devine
imposibil de corectat/debug-insideo-on-Real-Time. :wideeyed:
Tu gandeste-te daca (sizeof "* ribarm2droof.lsp") dimensiunea lui creste cu m*1ko, atunci tu sa corectezi  real-debug-in dificultatea lui/creste exponential la +65535...
 :-P
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 19, 2021, 05:21:32 AM
My latest revision that handles lastly posted DWG is posted here :
http://www.theswamp.org/index.php?topic=41837.msg603111#msg603111

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: BIGAL on January 19, 2021, 07:23:50 PM
Agree also where is the Administrators ?
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: wizman on January 19, 2021, 11:07:49 PM
Agree also where is the Administrators ?

Agree too.  His posts are incomprehensible.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ronjonp on January 19, 2021, 11:35:14 PM
AProgramul tau Lisp este de bun, dar daca tu cresti complexitatea ribarm2droof.lsp, atunci,  in viitor, programul tau nu va avea viitor, deoarece devine
imposibil de corectat/debug-insideo-on-Real-Time. :wideeyed:
Tu gandeste-te daca (sizeof "* ribarm2droof.lsp") dimensiunea lui creste cu m*1ko, atunci tu sa corectezi  real-debug-in dificultatea lui/creste exponential la +65535...
 :-P
I agree your posts are hard to follow ... here's a translation to English for those following.
Quote
Your Lisp program is good, but if you increase the complexity of ribarm2droof.lsp, then in the future your program will have no future because it becomes
impossible to correct / debug-insideo-on-Real-Time.
You think if (sizeof "* ribarm2droof.lsp") its size increases by m * 1ko, then you correct real-debug-in its difficulty / increase exponentially to +65535 ...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 18, 2021, 01:42:41 PM
Sorry for my late reply...

I just want to inform that I have improved existing routines posted by @chlh_jd... I have debugged it further more and tested and on ultimate roofs examples that are now solvable...
Also I want to mention that I posted my version also in archive, but still it's not so efficient and reliable as existing routine...
Still nevertheless, IMHO it is worh if nothing as a good example of different approach in coding and thinking...

Link for downloading is here :
https://www.cadtutor.net/forum/files/file/36-hipped-roof-routines/

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on October 09, 2021, 04:33:28 AM
I've updated archive... Few more files were added...
All relevant suggestions are welcomed...
You can download *.ZIP archive with *.LSP files from the link I posted in my previous reply...

Regards, M.R.

(P.S. Just checked, you may need to be logged on cadtutor to access new update...)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on June 27, 2022, 11:41:21 PM
Someone would think this is solved, but is it?

Proof ?
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 27, 2023, 07:04:43 AM
Hi...
I've started new one that is not so huge...
But it's only beginning - it creates only starting edges - triangles...
So now, if someone could step in and work for finishing, that'll be great...
Reason for my restart is slow behavior with big examples like ultimate roofs...
chlh_jd code is good, but sometimes it can't find correct exit combination and you waited pointlessly...

@Lee, @Daniel, @Gilles, @Evgeniy, @Owen, @Highflybird, ... and others, tell me how to continue, but not to loose speed too much...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:roof ( / *error* online inside-p mid bvecs car-sort dd barycent assocfuzz process unique test chklili loop pairs ss pl pli pll plll lwx lw lwxi lwi vecs li lil )
  2.  
  3.  
  4.   (defun *error* ( m )
  5.     (if
  6.       (and
  7.         lwi
  8.         (not (vlax-erased-p lwi))
  9.       )
  10.       (entdel lwi)
  11.     )
  12.     (if m
  13.       (prompt m)
  14.     )
  15.     (princ)
  16.   )
  17.  
  18.   (defun online ( p1 p2 p3 )
  19.     (equal (distance p1 p3) (+ (distance p1 p2) (distance p2 p3)) 1e-6)
  20.   )
  21.  
  22.   (defun inside-p ( p )
  23.   )
  24.  
  25.   (defun mid ( p1 p2 )
  26.     (mapcar
  27.       (function (lambda ( a b )
  28.         (/ (+ a b) 2.0)
  29.       ))
  30.       p1
  31.       p2
  32.     )
  33.   )
  34.  
  35.   (defun bvecs ( pl pli d )
  36.     (if (not pairs)
  37.       (setq pairs
  38.         (mapcar
  39.           (function (lambda ( a b )
  40.             (list a b)
  41.           ))
  42.           pl
  43.           (append (cdr pl) (list (car pl)))
  44.         )
  45.       )
  46.     )
  47.     (if pli
  48.       (setq vecs
  49.         (mapcar
  50.           (function (lambda ( x y )
  51.             (list x (polar x (angle x y) d))
  52.           ))
  53.           pl
  54.           pli
  55.         )
  56.       )
  57.     )
  58.     vecs
  59.   )
  60.  
  61.   (defun car-sort ( lst cmp / rtn )
  62.     (setq rtn (car lst))
  63.     (foreach itm (cdr lst)
  64.       (if (apply cmp (list itm rtn))
  65.         (setq rtn itm)
  66.       )
  67.     )
  68.     rtn
  69.   )
  70.  
  71.   (defun dd ( p / dl )
  72.     (foreach tt pairs
  73.       (setq dl
  74.         (cons
  75.           (distance
  76.             (mapcar (function +) (list 0.0 0.0) (trans p 1 (mapcar (function -) (cadr tt) (car tt))))
  77.             (mapcar (function +) (list 0.0 0.0) (trans (car tt) 1 (mapcar (function -) (cadr tt) (car tt))))
  78.           )
  79.           dl
  80.         )
  81.       )
  82.     )
  83.     dl
  84.   )
  85.  
  86.   (defun barycent ( pl )
  87.     (mapcar (function /)
  88.       (apply (function mapcar)
  89.         (cons (function +)
  90.           pl
  91.         )
  92.       )
  93.       (list (length pl) (length pl) (length pl))
  94.     )
  95.   )
  96.  
  97.   (defun assocfuzz ( itm lst fuzz )
  98.     (vl-some
  99.       (function (lambda ( x )
  100.         (if (equal itm (car x) fuzz) x)
  101.       ))
  102.       lst
  103.     )
  104.   )
  105.  
  106.   (defun process ( pl pli d / ips pln ii )
  107.     (if (not vecs)
  108.       (progn
  109.         (setq vecs
  110.           (bvecs pl pli d)
  111.         )
  112.         (setq vecs
  113.           (cons (last vecs)
  114.             (reverse
  115.               (cdr
  116.                 (reverse vecs)
  117.               )
  118.             )
  119.           )
  120.         )
  121.       )
  122.     )
  123.     (setq ipss
  124.       (mapcar
  125.         (function (lambda ( a b )
  126.           (list (car a) (cadr a) (inters (car a) (cadr a) (car b) (cadr b) nil) (car b) (cadr b))
  127.         ))
  128.         vecs
  129.         (append (cdr vecs) (list (car vecs)))
  130.       )
  131.     )
  132.     (setq ipss (vl-remove-if (function (lambda ( x ) (null (caddr x)))) ipss))
  133.     (setq ipss
  134.       (vl-remove-if
  135.         (function (lambda ( x / ddd )
  136.           (or
  137.             (not (inside-p (caddr x)))
  138.             (> 2 (- (length (setq ddd (dd (caddr x)))) (length (unique ddd))))
  139.             (vl-some
  140.               (function (lambda ( y )
  141.                 (online (car x) (caddr y) (caddr x))
  142.               ))
  143.               (vl-remove x ipss)
  144.             )
  145.           )
  146.         ))
  147.         ipss
  148.       )
  149.     )
  150.     (foreach ip ipss
  151.       (setq
  152.         lil
  153.         (cons
  154.           (list (car ip) (caddr ip))
  155.           lil
  156.         )
  157.         lil
  158.         (cons
  159.           (list (cadddr ip) (caddr ip))
  160.           lil
  161.         )
  162.       )
  163.       (while
  164.         (setq li
  165.           (vl-some
  166.             (function (lambda ( lii )
  167.               (if
  168.                 (and
  169.                   (online (car lii) (caddr ip) (cadr lii))
  170.                   (not (equal (car lii) (caddr ip) 1e-6))
  171.                   (not (equal (caddr ip) (cadr lii) 1e-6))
  172.                 )
  173.                 lii
  174.               )
  175.             ))
  176.             lil
  177.           )
  178.         )
  179.         (setq lil (vl-remove-if (function (lambda ( x ) (equal (cadr x) (cadr li) 1e-6))) lil))
  180.       )
  181.       (setq vecs (vl-remove (assocfuzz (car ip) vecs 1e-2) vecs))
  182.       (setq vecs (vl-remove (assocfuzz (cadddr ip) vecs 1e-2) vecs))
  183.       (setq
  184.         vecs
  185.         (cond
  186.           ( (and (/= 0 (vl-position (caddr ip) (mapcar (function caddr) ipss))) (/= (length pairs) (1+ (vl-position (caddr ip) (mapcar (function caddr) ipss)))))
  187.             (append
  188.               (list
  189.                 (list
  190.                   (caddr ip)
  191.                   (polar (caddr ip)
  192.                     (if
  193.                       (setq ii
  194.                         (inters
  195.                           (car (nth (1- (vl-position (caddr ip) (mapcar (function caddr) ipss))) pairs))
  196.                           (cadr (nth (1- (vl-position (caddr ip) (mapcar (function caddr) ipss))) pairs))
  197.                           (car (nth (1+ (vl-position (caddr ip) (mapcar (function caddr) ipss))) pairs))
  198.                           (cadr (nth (1+ (vl-position (caddr ip) (mapcar (function caddr) ipss))) pairs))
  199.                           nil
  200.                         )
  201.                       )
  202.                       (angle ii (caddr ip))
  203.                       (angle
  204.                         (barycent
  205.                           (list
  206.                             (car (nth (1- (vl-position (caddr ip) (mapcar (function caddr) ipss))) pairs))
  207.                             (cadr (nth (1- (vl-position (caddr ip) (mapcar (function caddr) ipss))) pairs))
  208.                             (car (nth (1+ (vl-position (caddr ip) (mapcar (function caddr) ipss))) pairs))
  209.                             (cadr (nth (1+ (vl-position (caddr ip) (mapcar (function caddr) ipss))) pairs))
  210.                           )
  211.                         ) (caddr ip)
  212.                       )
  213.                     )
  214.                     d
  215.                   )
  216.                 )
  217.               )
  218.               vecs
  219.             )
  220.           )
  221.           ( (= 0 (vl-position (caddr ip) (mapcar (function caddr) ipss)))
  222.             (append
  223.               (list
  224.                 (list
  225.                   (caddr ip)
  226.                   (polar (caddr ip)
  227.                     (if
  228.                       (setq ii
  229.                         (inters
  230.                           (car (last pairs))
  231.                           (cadr (last pairs))
  232.                           (car (nth (1+ (vl-position (caddr ip) (mapcar (function caddr) ipss))) pairs))
  233.                           (cadr (nth (1+ (vl-position (caddr ip) (mapcar (function caddr) ipss))) pairs))
  234.                           nil
  235.                         )
  236.                       )
  237.                       (angle ii (caddr ip))
  238.                       (angle
  239.                         (barycent
  240.                           (list
  241.                             (car (last pairs))
  242.                             (cadr (last pairs))
  243.                             (car (nth (1+ (vl-position (caddr ip) (mapcar (function caddr) ipss))) pairs))
  244.                             (cadr (nth (1+ (vl-position (caddr ip) (mapcar (function caddr) ipss))) pairs))
  245.                           )
  246.                         ) (caddr ip)
  247.                       )
  248.                     )
  249.                     d
  250.                   )
  251.                 )
  252.               )
  253.               vecs
  254.             )
  255.           )
  256.           ( (= (length pairs) (1+ (vl-position (caddr ip) (mapcar (function caddr) ipss))))
  257.             (append
  258.               (list
  259.                 (list
  260.                   (caddr ip)
  261.                   (polar (caddr ip)
  262.                     (if
  263.                       (setq ii
  264.                         (inters
  265.                           (car (nth (1- (vl-position (caddr ip) (mapcar (function caddr) ipss))) pairs))
  266.                           (cadr (nth (1- (vl-position (caddr ip) (mapcar (function caddr) ipss))) pairs))
  267.                           (car (nth (1+ (vl-position (caddr ip) (mapcar (function caddr) ipss))) (append pairs pairs)))
  268.                           (cadr (nth (1+ (vl-position (caddr ip) (mapcar (function caddr) ipss))) (append pairs pairs)))
  269.                           nil
  270.                         )
  271.                       )
  272.                       (angle ii (caddr ip))
  273.                       (angle
  274.                         (barycent
  275.                           (list
  276.                             (car (nth (1- (vl-position (caddr ip) (mapcar (function caddr) ipss))) pairs))
  277.                             (cadr (nth (1- (vl-position (caddr ip) (mapcar (function caddr) ipss))) pairs))
  278.                             (car (nth (1+ (vl-position (caddr ip) (mapcar (function caddr) ipss))) (append pairs pairs)))
  279.                             (cadr (nth (1+ (vl-position (caddr ip) (mapcar (function caddr) ipss))) (append pairs pairs)))
  280.                           )
  281.                         ) (caddr ip)
  282.                       )
  283.                     )
  284.                     d
  285.                   )
  286.                 )
  287.               )
  288.               vecs
  289.             )
  290.           )
  291.         )
  292.       )
  293.       (if (assocfuzz (car ip) vecs 1e-6)
  294.         (setq vecs (vl-remove-if (function (lambda ( x ) (equal (car x) (car ip) 1e-6))) vecs))
  295.       )
  296.       (if (assocfuzz (cadddr ip) vecs 1e-6)
  297.         (setq vecs (vl-remove-if (function (lambda ( x ) (equal (car x) (cadddr ip) 1e-6))) vecs))
  298.       )
  299.     )
  300.     (mapcar (function car) vecs)
  301.   )
  302.  
  303.   (defun unique ( lst / a ll )
  304.     (while (setq a (car lst))
  305.       (if (vl-some (function (lambda ( x ) (equal x a 1e-6))) (cdr lst))
  306.         (setq ll (cons a ll) lst (vl-remove-if (function (lambda ( x ) (equal x a 1e-6))) (cdr lst)))
  307.         (setq ll (cons a ll) lst (cdr lst))
  308.       )
  309.     )
  310.     (reverse ll)
  311.   )
  312.  
  313.   (defun test nil
  314.     (and
  315.       (vl-every
  316.         (function (lambda ( x )
  317.           (vl-some
  318.             (function (lambda ( y )
  319.               (equal x y 1e-6)
  320.             ))
  321.             (apply (function append) lil)
  322.           )
  323.         ))
  324.         pl
  325.       )
  326.       (not
  327.         (vl-some
  328.           (function (lambda ( li1 )
  329.             (vl-some
  330.               (function (lambda ( li2 / ip )
  331.                 (and
  332.                   (setq ip (inters (car li1) (cadr li1) (car li2) (cadr li2)))
  333.                   (not (equal ip (car li1) 1e-6))
  334.                   (not (equal ip (cadr li1) 1e-6))
  335.                   (not (equal ip (car li2) 1e-6))
  336.                   (not (equal ip (cadr li2) 1e-6))
  337.                 )
  338.               ))
  339.               (vl-remove li1 lil)
  340.             )
  341.           ))
  342.           lil
  343.         )
  344.       )
  345.     )
  346.   )
  347.  
  348.   (defun chklili ( lil / lilpts )
  349.     (setq lilpts (apply (function append) lil))
  350.     (setq lilpts (vl-remove-if-not (function (lambda ( x ) (= (length (vl-remove-if (function (lambda ( y ) (equal x y 1e-6))) lilpts)) (- (length lilpts) 2)))) lilpts))
  351.     lilpts
  352.   )
  353.  
  354.   (prompt "\nPick closed polygonal LWPOLYLINE...")
  355.   (if
  356.     (setq ss
  357.       (ssget "_+.:E:S" (list (cons 0 "LWPOLYLINE") (cons -4 "&=") (cons 70 1) (cons -4 "<not") (cons -4 "<>") (cons 42 0.0) (cons -4 "not>")))
  358.     )
  359.     (progn
  360.       (setq pl
  361.         (mapcar (function cdr)
  362.           (vl-remove-if
  363.             (function (lambda ( x )
  364.               (/= (car x) 10)
  365.             ))
  366.             (setq lwx
  367.               (entget
  368.                 (setq lw
  369.                   (ssname ss 0)
  370.                 )
  371.               )
  372.             )
  373.           )
  374.         )
  375.       )
  376.       (setq lwi (vlax-vla-object->ename (car (vlax-invoke (vlax-ename->vla-object lw) 'offset 1e-2))))
  377.       (if (> (vlax-curve-getarea lwi) (vlax-curve-getarea lw))
  378.         (progn
  379.           (entdel lwi)
  380.           (setq lwi (vlax-vla-object->ename (car (vlax-invoke (vlax-ename->vla-object lw) 'offset -1e-2))))
  381.         )
  382.       )
  383.       (setq pli
  384.         (mapcar (function cdr)
  385.           (vl-remove-if
  386.             (function (lambda ( x )
  387.               (/= (car x) 10)
  388.             ))
  389.             (setq lwxi
  390.               (entget lwi)
  391.             )
  392.           )
  393.         )
  394.       )
  395.       (setq loop t)
  396.       (while (and loop (setq pll (process pl pli 1.0)))
  397.         (if
  398.           (or
  399.             ;(test)
  400.             ;(not (chklili lil))
  401.             (<= (cdr (assoc 90 lwx)) (length lil))
  402.             (equal pll plll 1e-6)
  403.           )
  404.           (setq loop nil)
  405.         )
  406.         (setq plll pll pli nil)
  407.       )
  408.       (foreach li lil
  409.         (entmake
  410.           (list
  411.             (cons 0 "LINE")
  412.             (cons 10 (car li))
  413.             (cons 11 (cadr li))
  414.           )
  415.         )
  416.       )
  417.     )
  418.   )
  419.   (*error* nil)
  420. )
  421.  

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: pkohut on February 27, 2023, 07:41:01 AM


@Lee, @Daniel, @Gilles, @Evgeniy, @Owen, @Highflybird, ... and others, tell me how to continue, but not to loose speed too much...

Where are the code comments?
Want speed - get out of lisp.
Want speed - keep nested loops to a minimum.
Want speed - don't do heavy calculations deep in the loops.
Want speed - don't do if checks deep in the loops.
Want speed - learn algorithms and data structures.

Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 27, 2023, 07:58:43 AM


@Lee, @Daniel, @Gilles, @Evgeniy, @Owen, @Highflybird, ... and others, tell me how to continue, but not to loose speed too much...

Where are the code comments?
Want speed - get out of lisp.
Want speed - keep nested loops to a minimum.
Want speed - don't do heavy calculations deep in the loops.
Want speed - don't do if checks deep in the loops.
Want speed - learn algorithms and data structures.



Why do you need code comments?
For other opinions I agree, but not my field of knowledge...
It could be written in lisp for sure to be fast enough - look at chlh_jd's example...
The problem is that complexity may grow - ultimate roofs - 100 and more vertices...

If you test it, you'll see what it do till now... The problem arises with widening code - finding correct solution based on offsetting...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 28, 2023, 01:19:58 PM
And here is it classical example of how topic get overcrowded with other new ones...

Have someone thought ab this examples, especially the ones posted at cadtutor.net - download section...

I would like to see something new that may be better than my attempts...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: pkohut on February 28, 2023, 02:48:52 PM
And here is it classical example of how topic get overcrowded with other new ones...

Have someone thought ab this examples, especially the ones posted at cadtutor.net - download section...

I would like to see something new that may be better than my attempts...

M.R.

Fair enough.  Your playground, your rules.  Take my non-lisp ball to another field.   :2funny:
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on March 01, 2023, 10:18:03 AM
 :-D :knuppel2:
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on March 06, 2023, 10:53:05 AM
To all users, or else watching...

Can this routine here in attachment work faster with ultimate roof example, and not to depend on PC hardware architecture configuration...

I am not searching translation to ARX, BRX, (but I suppose it would be perfect) or something else, though and DLL for latest versions of Auto/BricsCAD would be very welcomed, I also need tweaks if someone operates with *.LSP on higher level of intelligence...

Link for ultimate roof DWG : https://www.theswamp.org/index.php?topic=41837.msg613477#msg613477
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on March 06, 2023, 12:11:08 PM
Letter to programmer masters...
Quote
Is my routine hardware dependent... (roof2d-new-new-offset.lsp)... If so, which is mostly the case, then the job is done... I don't know how to tweak for ultimate roofs (100 and more vertices)... If it doesn't depend on the hardware, then let someone more expert than me take a look... It doesn't matter if it's *.lsp, *.arx, *.brx, * .dll... it's only important that it works optimally, which I doubt... I don't know, if you care, ask around, maybe by chance a solution for slowness will be found... I took all the parameters into circulation and matched them with each other ... Perhaps I should have left something out, but then not all the tests would have passed... See what you can do, if you have the time and interest...

Testing *.DWG is in attachment...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on March 07, 2023, 10:35:30 AM
 :-( :-( :-(
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on March 11, 2023, 11:56:24 AM
I've found some workaround... It was all the time in front of my(ours) nose...
As for the ultimate roofs, test for 1 full rotation of vertices, so if it is not found the first time, trim one of the sides of the lwpoly, turn it closed through palette, which changes the initial vertex... After a bit of hacking in this way, 2droof-final.lsp should find solution within 1 to 2 seconds... Just don't forget that errm doesn't even need to iterate... The solution should be solvable during the first iteration of errn...

New update is here : https://www.cadtutor.net/forum/files/file/36-hipped-roof-routines/
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on July 20, 2023, 02:02:40 PM
I had some spare time, so I've coded for smaller more concise version... But how story goes, what is short and quick it's more lackable... Still if someone want's to connect and improve, here is it...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:roof-new ( / *error* unit rlw offd inside-p collinear-p unique chkcircinside mc subprocess1 subprocess2 subprocess process done lw lwi lwx lwo ent enti lil lix lwnl ch el p1 p2 pp ppp ipp ippl vll vlli tll iplst iplstt iplsttt lst lstt ) ;;; cad, doc, spc, lay, vlll, vllli, tlll, tmp, f - global variables ;;;
  2.  
  3.  
  4.   (defun *error* ( m )
  5.     (if (and lwi (not (vlax-erased-p lwi)))
  6.       (entdel lwi)
  7.     )
  8.     (if (and enti (not (vlax-erased-p enti)))
  9.       (entdel enti)
  10.     )
  11.     (if (setq ppp (unique ppp))
  12.       (foreach pp ppp
  13.         (if (not (vl-some '(lambda ( x ) (and (not (equal (car x) pp 1e-6)) (not (equal (cadr x) pp 1e-6)) (not (equal (car x) (cadr x) 1e-6)) (collinear-p (car x) pp (cadr x)))) lil))
  14.           (entmake (list (cons 0 "POINT") (cons 10 pp) (cons 62 1)))
  15.         )
  16.       )
  17.     )
  18.     (if (= ch "No")
  19.       (while (setq el (entnext el))
  20.         (if (and el (not (vlax-erased-p el)))
  21.           (entdel el)
  22.         )
  23.       )
  24.     )
  25.     (if lil
  26.       (foreach li lil
  27.         (if (vl-some '(lambda ( x ) (and (not (equal (car li) x 1e-6)) (not (equal (cadr li) x 1e-6)) (not (equal (car li) (cadr li) 1e-6)) (collinear-p (car li) x (cadr li)))) ppp)
  28.           (setq lil (vl-remove li lil))
  29.           (if (vl-some '(lambda ( x ) (equal x (car li) 1e-6)) ppp)
  30.             (entmake (list (cons 0 "LINE") (cons 10 (car li)) (cons 11 (cadr li)) (cons 62 3)))
  31.           )
  32.         )
  33.       )
  34.     )
  35.     (command-s "_.-OVERKILL" "_ALL" "_T" "_Y" "" "")
  36.     (if (= 8 (logand 8 (getvar 'undoctl)))
  37.       (vla-endundomark doc)
  38.     )
  39.     (prompt "\nIf you want to start again on different sample, make sure you nil flag f : (setq f nil vlll nil vllli nil tlll nil tmp nil) ...")
  40.     (if m
  41.       (prompt m)
  42.     )
  43.     (princ)
  44.   )
  45.  
  46.   (defun unit ( v / d )
  47.     (if (not (equal (setq d (distance '(0.0 0.0 0.0) v)) 0.0 1e-6))
  48.       (mapcar '(lambda ( x ) (/ x d)) v)
  49.     )
  50.   )
  51.  
  52.   (defun rlw ( lw / e x1 x2 x3 x4 x5 x6 )
  53.     ;; by ElpanovEvgeniy
  54.     (if (= (cdr (assoc 0 (setq e (entget lw)))) "LWPOLYLINE")
  55.       (progn
  56.         (foreach a1 e
  57.           (cond ((= (car a1) 10) (setq x2 (cons a1 x2)))
  58.                 ((= (car a1) 40) (setq x4 (cons (cons 41 (cdr a1)) x4)))
  59.                 ((= (car a1) 41) (setq x3 (cons (cons 40 (cdr a1)) x3)))
  60.                 ((= (car a1) 42) (setq x5 (cons (cons 42 (- (cdr a1))) x5)))
  61.                 ((= (car a1) 210) (setq x6 (cons a1 x6)))
  62.                 (t (setq x1 (cons a1 x1)))
  63.           )
  64.         )
  65.         (entmod (append (reverse x1)
  66.                   (append (apply 'append
  67.                             (apply 'mapcar
  68.                               (cons 'list
  69.                                 (list x2
  70.                                   (cdr (reverse (cons (car x3) (reverse x3))))
  71.                                   (cdr (reverse (cons (car x4) (reverse x4))))
  72.                                   (cdr (reverse (cons (car x5) (reverse x5))))
  73.                                 )
  74.                               )
  75.                             )
  76.                           )
  77.                           x6
  78.                   )
  79.                 )
  80.         )
  81.         (entupd lw)
  82.       )
  83.     )
  84.   )
  85.  
  86.   (defun offd ( sign ip tl / dl )
  87.     (setq dl (mapcar '(lambda ( x )
  88.                         (distance ip
  89.                           (inters
  90.                             ip
  91.                             (polar ip (+ (* 0.5 pi) (angle (car x) (cadr x))) 1.0)
  92.                             (car x)
  93.                             (polar (car x) (angle (car x) (cadr x)) 1.0)
  94.                             nil
  95.                           )
  96.                         )
  97.                       ) tl
  98.               )
  99.     )
  100.     (vl-some '(lambda ( x )
  101.                (if
  102.                  (>
  103.                    (-
  104.                      (length dl)
  105.                      (length (vl-remove-if '(lambda ( y ) (equal x y 1.0)) dl))
  106.                    ) 2
  107.                  )
  108.                  x
  109.                )
  110.              ) (vl-sort dl sign)
  111.     )
  112.   )
  113.  
  114.   (defun inside-p ( p lw lwi )
  115.   )
  116.  
  117.   (defun collinear-p ( p1 p p2 )
  118.     (equal (distance p1 p2) (+ (distance p1 p) (distance p p2)) 1e-6)
  119.   )
  120.  
  121.   (defun unique ( pl )
  122.     (if pl
  123.       (cons (car pl)
  124.             (unique
  125.               (vl-remove-if
  126.                 '(lambda ( x )
  127.                    (equal x (car pl) 1e-6)
  128.                  )
  129.                 (cdr pl)
  130.               )
  131.             )
  132.       )
  133.     )
  134.   )
  135.  
  136.   (defun chkcircinside ( pp tll / d ci ipp ippl tst )
  137.     (if (and pp tll)
  138.       (progn
  139.         (setq ci (entmakex (list (cons 0 "CIRCLE") (cons 10 pp) (cons 40 (offd (function <) pp tll)))))
  140.         (if (setq ipp (vlax-invoke (vlax-ename->vla-object ci) 'intersectwith (vlax-ename->vla-object ent) acextendnone))
  141.           (progn
  142.             (while ipp
  143.               (setq ippl (cons (list (car ipp) (cadr ipp) (caddr ipp)) ippl))
  144.               (setq ipp (cdddr ipp))
  145.             )
  146.             (if (or (not ippl) (vl-every '(lambda ( x ) (inside-p x ent enti)) (apply 'append (mapcar '(lambda ( x ) (list (vlax-curve-getpointatparam ci (- x 0.1)) (vlax-curve-getpointatparam ci (+ x 0.1)))) (mapcar '(lambda ( x ) (vlax-curve-getparamatpoint ci x)) ippl)))))
  147.               (setq tst t)
  148.             )
  149.           )
  150.           (setq tst t)
  151.         )
  152.         (if (and ci (not (vlax-erased-p ci)))
  153.           (entdel ci)
  154.         )
  155.       )
  156.     )
  157.     tst
  158.   )
  159.  
  160.   (defun mc ( p lw / ci pl mp )
  161.     (setq ci (entmakex (list (cons 0 "CIRCLE") (cons 10 p) (cons 40 1.0))))
  162.     (setq pl (vlax-invoke (vlax-ename->vla-object ci) 'intersectwith (vlax-ename->vla-object lw) acextendnone))
  163.     (if (and ci (not (vlax-erased-p ci)))
  164.       (entdel ci)
  165.     )
  166.     (setq mp (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) (list (car pl) (cadr pl)) (list (nth 3 pl) (nth 4 pl))))
  167.     (list p mp)
  168.   )
  169.  
  170.   (defun subprocess1 ( vl vli / ip ) ;;; iplst - lexical global variable
  171.     (setq iplst (mapcar '(lambda ( p1 p2 p3 p4 / ip )
  172.                           (if
  173.                             (and
  174.                               (setq ip (inters p1 p2 p3 p4 nil))
  175.                               (inside-p ip ent enti)
  176.                               (equal (unit (mapcar '- ip p1)) (unit (mapcar '- p2 p1)) 1e-6)
  177.                               (equal (unit (mapcar '- ip p3)) (unit (mapcar '- p4 p3)) 1e-6)
  178.                               (inside-p (mapcar '+ p1 (mapcar '* (unit (mapcar '- ip p1)) (list 1e-2 1e-2))) ent enti)
  179.                               (inside-p (mapcar '+ p3 (mapcar '* (unit (mapcar '- ip p3)) (list 1e-2 1e-2))) ent enti)
  180.                             )
  181.                             (list ip (list p1 p3))
  182.                             (list nil nil)
  183.                           )
  184.                         )
  185.                         vl
  186.                         vli
  187.                         (append (cdr vl) (list (car vl)))
  188.                         (append (cdr vli) (list (car vli)))
  189.                 )
  190.     )
  191.   )
  192.  
  193.   (defun subprocess2 ( p lw vl vli / ip lst lstt ) ;;; iplst - lexical global variable
  194.     (setq iplst (mapcar '(lambda ( p1 p2 p3 p4 / ip )
  195.                           (if
  196.                             (and
  197.                               (setq ip (inters p1 p2 p3 p4 nil))
  198.                               (inside-p ip ent enti)
  199.                               (equal (unit (mapcar '- ip p1)) (unit (mapcar '- p2 p1)) 1e-6)
  200.                               (equal (unit (mapcar '- ip p3)) (unit (mapcar '- p4 p3)) 1e-6)
  201.                               (inside-p (mapcar '+ p1 (mapcar '* (unit (mapcar '- ip p1)) (list 1e-2 1e-2))) ent enti)
  202.                               (inside-p (mapcar '+ p3 (mapcar '* (unit (mapcar '- ip p3)) (list 1e-2 1e-2))) ent enti)
  203.                             )
  204.                             (list ip (list p1 p3))
  205.                             (list nil nil)
  206.                           )
  207.                         )
  208.                         vl
  209.                         vli
  210.                         (repeat (length vl) (setq lst (cons (car (mc p lw)) lst)))
  211.                         (repeat (length vl) (setq lstt (cons (cadr (mc p lw)) lstt)))
  212.                 )
  213.     )
  214.   )
  215.  
  216.   (defun subprocess ( iplst / p1 p2 pp1 pp2 k i dd )
  217.     (setq iplst (vl-remove-if '(lambda ( x ) (or (null (car x)) (null (cadr x)))) iplst))
  218.     (setq iplstoffd (mapcar '(lambda ( x ) (if (and (car x) (cadr x))
  219.                                              (list
  220.                                                (mapcar '+ '(0.0 0.0) (car x))
  221.                                                (mapcar '(lambda ( y ) (mapcar '+ '(0.0 0.0) y)) (cadr x))
  222.                                                (offd (function <) (mapcar '+ '(0.0 0.0) (car x)) tlll)
  223.                                              )
  224.                                              (list nil nil nil)
  225.                                            )
  226.                              ) iplst
  227.                     )
  228.     )
  229.     (setq iplstoffd (vl-remove-if '(lambda ( x ) (null (caddr x))) iplstoffd))
  230.     (setq iplstoffd (vl-sort iplstoffd '(lambda ( a b ) (< (caddr a) (caddr b)))))
  231.     (foreach ipd iplstoffd
  232.       (if (not tmp)
  233.         (setq tmp (offd (function <) (car ipd) tlll))
  234.       )
  235.       (if (and ipd (chkcircinside (car ipd) tlll) (<= tmp (setq tmp (offd (function <) (car ipd) tlll))))
  236.         (progn
  237.           (setq ppp (cons (car ipd) ppp))
  238.           (if
  239.             (and
  240.               (setq p1
  241.                 (vl-some '(lambda ( x )
  242.                   (if
  243.                     (equal
  244.                       (unit (mapcar '- (car ipd) x))
  245.                       (unit (mapcar '- (car ipd) (caadr ipd)))
  246.                       1e-6
  247.                     )
  248.                     x
  249.                   )
  250.                 ) vll
  251.                 )
  252.               )
  253.               (setq pp1
  254.                 (vl-some '(lambda ( x )
  255.                   (if
  256.                     (equal
  257.                       (unit (mapcar '- (car ipd) x))
  258.                       (unit (mapcar '- (car ipd) p1))
  259.                       1e-6
  260.                     )
  261.                     x
  262.                   )
  263.                 ) vlll
  264.                 )
  265.               )
  266.             )
  267.             (setq lil (cons (list (car ipd) pp1) lil))
  268.             (if p1
  269.               (setq lil (cons (list (car ipd) p1) lil))
  270.               (setq lil (cons (list (car ipd) (caadr ipd)) lil))
  271.             )
  272.           )
  273.           (if
  274.             (and
  275.               (setq p2
  276.                 (vl-some '(lambda ( x )
  277.                   (if
  278.                     (equal
  279.                       (unit (mapcar '- (car ipd) x))
  280.                       (unit (mapcar '- (car ipd) (cadadr ipd)))
  281.                       1e-6
  282.                     )
  283.                     x
  284.                   )
  285.                 ) vll
  286.                 )
  287.               )
  288.               (setq pp2
  289.                 (vl-some '(lambda ( x )
  290.                   (if
  291.                     (equal
  292.                       (unit (mapcar '- (car ipd) x))
  293.                       (unit (mapcar '- (car ipd) p2))
  294.                       1e-6
  295.                     )
  296.                     x
  297.                   )
  298.                 ) vlll
  299.                 )
  300.               )
  301.             )
  302.             (setq lil (cons (list (car ipd) pp2) lil))
  303.             (if p2
  304.               (setq lil (cons (list (car ipd) p2) lil))
  305.               (setq lil (cons (list (car ipd) (cadadr ipd)) lil))
  306.             )
  307.           )
  308.           (setq k 1e-4 i 0)
  309.           (while
  310.             (and
  311.               (caddr ipd)
  312.               (setq dd (- (caddr ipd) (* k (setq i (1+ i)))))
  313.               (setq lwnl (vl-catch-all-apply
  314.                 'vlax-invoke
  315.                 (list (vlax-ename->vla-object lw) 'offset (- dd))
  316.               ))
  317.               (if (vl-catch-all-error-p lwnl)
  318.                 (/= i 10)
  319.               )
  320.             )
  321.           )
  322.           (if (not (vl-catch-all-error-p lwnl))
  323.             (setq lwnl (mapcar 'vlax-vla-object->ename lwnl))
  324.             lwnl
  325.           )
  326.           (if (and (not (vl-catch-all-error-p lwnl)) (= (length lwnl) 1))
  327.             (setq iplstt (cons (list (car ipd) (car lwnl)) iplstt))
  328.           )
  329.         )
  330.       )
  331.     )
  332.     (setq tmp nil)
  333.     lwnl
  334.   )
  335.  
  336.   (defun process ( lw / lwi lwx vl vli iplst tl ipd iplstoffd p1 p2 pp lwnl ) ;;; vll ; vlli ; tll ; lwnl ; ppp ; iplstt - lexical global variables ;;;
  337.     (if lw
  338.       (progn
  339.         (setq vl (mapcar 'cdr (vl-remove-if '(lambda ( x ) (/= (car x) 10)) (setq lwx (entget lw)))))
  340.         (if (not vll)
  341.           (setq vll vl)
  342.         )
  343.         (setq lwi (vlax-vla-object->ename (car (vlax-invoke (vlax-ename->vla-object lw) 'offset -1e-3))))
  344.         (setq vli (mapcar 'cdr (vl-remove-if '(lambda ( x ) (/= (car x) 10)) (entget lwi))))
  345.         (if (not vlli)
  346.           (setq vlli vli)
  347.         )
  348.         (setq tl (mapcar '(lambda ( a b ) (list a b)) vl (append (cdr vl) (list (car vl)))))
  349.         (if (not tll)
  350.           (setq tll tl)
  351.         )
  352.         (setq iplst (subprocess1 vl vli))
  353.         (subprocess iplst)
  354.         (setq iplst nil)
  355.         (foreach pp iplstt
  356.           (setq iplsttt (append iplsttt (subprocess2 (car pp) (cadr pp) vl vli)))
  357.         )
  358.         (subprocess iplsttt)
  359.         (if (and lwi (not (vlax-erased-p lwi)))
  360.           (entdel lwi)
  361.         )
  362.         lwnl
  363.       )
  364.     )
  365.   )
  366.  
  367.   (or doc (setq doc (vla-get-activedocument cad)))
  368.   (or spc (setq spc (vla-get-block (setq lay (vla-get-activelayout doc)))))
  369.  
  370.   (if (= 8 (logand 8 (getvar 'undoctl)))
  371.     (vla-endundomark doc)
  372.   )
  373.   (if
  374.     (and
  375.       (setq lw (car (entsel "\nPick boundary closed polygonal LWPOLYLINE with only straight segments...")))
  376.       (= (cdr (assoc 0 (setq lwx (entget lw)))) "LWPOLYLINE")
  377.       (= 1 (logand 1 (cdr (assoc 70 lwx))))
  378.       (vl-every '(lambda ( x ) (= 0.0 (cdr x))) (vl-remove-if '(lambda ( x ) (/= (car x) 42)) lwx))
  379.       (setq el (entlast) ent lw)
  380.     )
  381.     (progn
  382.       (setq lwi (vlax-vla-object->ename (car (vlax-invoke (vlax-ename->vla-object lw) 'offset -1e-3))))
  383.       (if (> (vlax-curve-getarea lwi) (vlax-curve-getarea lw))
  384.         (setq lw (rlw lw)) ;;; force main lwpolyline CCW - counter clockwise ;;;
  385.       )
  386.       (setq enti (vlax-vla-object->ename (car (vlax-invoke (vlax-ename->vla-object ent) 'offset -1e-3))))
  387.       (if (and lwi (not (vlax-erased-p lwi)))
  388.         (entdel lwi)
  389.       )
  390.       (if (not f)
  391.         (progn
  392.           (setq vlll (mapcar 'cdr (vl-remove-if '(lambda ( x ) (/= (car x) 10)) (entget ent))))
  393.           (setq vllli (mapcar 'cdr (vl-remove-if '(lambda ( x ) (/= (car x) 10)) (entget enti))))
  394.           (setq tlll (mapcar '(lambda ( a b ) (list a b)) vlll (append (cdr vlll) (list (car vlll)))))
  395.           (setq f t)
  396.         )
  397.       )
  398.       (initget "Yes No")
  399.       (setq ch (cond ((getkword "\nDo you want points-offsets-lines or just lines [Yes / No] <Yes> - there are some lacks with just lines, so HIT ENTER : ")) ( "Yes" )))
  400.       (while (not done)
  401.         (if
  402.           (and
  403.             (not (vl-catch-all-error-p (setq lwnl (vl-catch-all-apply 'process (list lw)))))
  404.             lwnl
  405.           )
  406.           (while (and (not done) (setq lww (car lwnl)))
  407.             (setq lwnl (vl-remove lww lwnl))
  408.             (setq lwo lww)
  409.             (setq lwnl (vl-catch-all-apply 'process (list lww)))
  410.             (if (vl-catch-all-error-p lwnl)
  411.               (setq done t)
  412.               (if (eq (car lwnl) lwo)
  413.                 (setq done t)
  414.               )
  415.             )
  416.           )
  417.           (setq done t)
  418.         )
  419.       ) ;;; main engine ;;;
  420.     )
  421.   )
  422.   (*error* nil)
  423. )
  424.  

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on July 21, 2023, 11:26:03 AM
The lack that I see now and before is that that's built-in offset command... When I offset inside through point for example drawn inside point "P", offset LWPOLYLINE may pass and may not pass through and in that case usually it breaks to a few more smaller LWPOLYLINES...
So now problem consist in solving this task, upon solving and applying in my lastly posted code, solution should be successfuly created...

Any ideas are very welcomed...
Thanks for reading, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on July 21, 2023, 12:41:49 PM
I've created sub (offset-inside), but it doesn't help, especially as I don't quite know how to combine it with real offset command function...

Code: [Select]
THE CODE IS SOMEWHAT WORSE THAN PREVIOUSLY POSTED, SO I REMOVED IT FROM SITE...

So, who want's to find solution in his own manner is welcomed to step in...
M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 03, 2023, 02:04:42 PM
I just wanted to put this topic back at top of list so it remains as not solved challenge... It should work fast and follow offset inside paths...
My latest version is example of short and efficient code, but it just gives partial ridge lines... Like I said, someone with more experiences should step in and try to correct it...
So long from me...
See you these days if you have something...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 07, 2023, 02:32:23 PM
I don't see any feedback...
What should I change in posted code... (C:ROOF-NEW)

Thanks for your advices if there are any newly ones...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 08, 2023, 01:30:57 AM
I've changed a bit, so now it looks better, but it goes in infinite loops and doesn't do what I predicted - from smallest offset then first just a little larger offset where points are met, and so on, so on... It seems that this topic is not so interesting, but now I feel that I am close to finish correct... It's just that PC doesn't want to operate as expected...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:roof-new-new ( / *error* unit rlw offd inside-p collinear-p unique chkcircinside mc preprocess1 preprocess2 rem-vllvlli subprocess process done lw lwi lwx lwo ent enti lil lix lwnl ch el p1 p2 pp ppp ipp ippl vll vlli tll iplst lst ) ;;; cad, doc, spc, lay, vlll, vllli, tlll, f - global variables ;;;
  2.  
  3.  
  4.   (defun *error* ( m )
  5.     (if (and lwi (not (vlax-erased-p lwi)))
  6.       (entdel lwi)
  7.     )
  8.     (if (and enti (not (vlax-erased-p enti)))
  9.       (entdel enti)
  10.     )
  11.     (if (setq ppp (unique ppp))
  12.       (foreach pp ppp
  13.         (if (not (vl-some '(lambda ( x ) (and (not (equal (car x) pp 1e-6)) (not (equal (cadr x) pp 1e-6)) (not (equal (car x) (cadr x) 1e-6)) (collinear-p (car x) pp (cadr x)))) lil))
  14.           (entmake (list (cons 0 "POINT") (cons 10 pp) (cons 62 1)))
  15.         )
  16.       )
  17.     )
  18.     (if (= ch "No")
  19.       (while (setq el (entnext el))
  20.         (if (and el (not (vlax-erased-p el)))
  21.           (entdel el)
  22.         )
  23.       )
  24.     )
  25.     (if lil
  26.       (foreach li lil
  27.         (if (vl-some '(lambda ( x ) (and (not (equal (car li) x 1e-6)) (not (equal (cadr li) x 1e-6)) (not (equal (car li) (cadr li) 1e-6)) (collinear-p (car li) x (cadr li)))) ppp)
  28.           (setq lil (vl-remove li lil))
  29.           (if (vl-some '(lambda ( x ) (equal x (car li) 1e-6)) ppp)
  30.             (entmake (list (cons 0 "LINE") (cons 10 (car li)) (cons 11 (cadr li)) (cons 62 3)))
  31.           )
  32.         )
  33.       )
  34.     )
  35.     (command-s "_.-OVERKILL" "_ALL" "_T" "_Y" "" "")
  36.     (if (= 8 (logand 8 (getvar 'undoctl)))
  37.       (vla-endundomark doc)
  38.     )
  39.     (prompt "\nIf you want to start again on different sample, make sure you nil flag f : (setq f nil vlll nil vllli nil tlll nil) ...")
  40.     (if m
  41.       (prompt m)
  42.     )
  43.     (princ)
  44.   )
  45.  
  46.   (defun unit ( v / d )
  47.     (if (not (equal (setq d (distance '(0.0 0.0 0.0) v)) 0.0 1e-6))
  48.       (mapcar '(lambda ( x ) (/ x d)) v)
  49.     )
  50.   )
  51.  
  52.   (defun rlw ( lw / e x1 x2 x3 x4 x5 x6 )
  53.     ;; by ElpanovEvgeniy
  54.     (if (= (cdr (assoc 0 (setq e (entget lw)))) "LWPOLYLINE")
  55.       (progn
  56.         (foreach a1 e
  57.           (cond ((= (car a1) 10) (setq x2 (cons a1 x2)))
  58.                 ((= (car a1) 40) (setq x4 (cons (cons 41 (cdr a1)) x4)))
  59.                 ((= (car a1) 41) (setq x3 (cons (cons 40 (cdr a1)) x3)))
  60.                 ((= (car a1) 42) (setq x5 (cons (cons 42 (- (cdr a1))) x5)))
  61.                 ((= (car a1) 210) (setq x6 (cons a1 x6)))
  62.                 (t (setq x1 (cons a1 x1)))
  63.           )
  64.         )
  65.         (entmod (append (reverse x1)
  66.                   (append (apply 'append
  67.                             (apply 'mapcar
  68.                               (cons 'list
  69.                                 (list x2
  70.                                   (cdr (reverse (cons (car x3) (reverse x3))))
  71.                                   (cdr (reverse (cons (car x4) (reverse x4))))
  72.                                   (cdr (reverse (cons (car x5) (reverse x5))))
  73.                                 )
  74.                               )
  75.                             )
  76.                           )
  77.                           x6
  78.                   )
  79.                 )
  80.         )
  81.         (entupd lw)
  82.       )
  83.     )
  84.   )
  85.  
  86.   (defun offd ( sign ip tl / dl )
  87.     (setq dl (mapcar '(lambda ( x )
  88.                         (distance ip
  89.                           (inters
  90.                             ip
  91.                             (polar ip (+ (* 0.5 pi) (angle (car x) (cadr x))) 1.0)
  92.                             (car x)
  93.                             (polar (car x) (angle (car x) (cadr x)) 1.0)
  94.                             nil
  95.                           )
  96.                         )
  97.                       ) tl
  98.               )
  99.     )
  100.     (vl-some '(lambda ( x )
  101.                (if
  102.                  (>
  103.                    (-
  104.                      (length dl)
  105.                      (length (vl-remove-if '(lambda ( y ) (equal x y 0.05)) dl))
  106.                    ) 2
  107.                  )
  108.                  x
  109.                )
  110.              ) (vl-sort dl sign)
  111.     )
  112.   )
  113.  
  114.   (defun inside-p ( p lw lwi )
  115.   )
  116.  
  117.   (defun collinear-p ( p1 p p2 )
  118.     (equal (distance p1 p2) (+ (distance p1 p) (distance p p2)) 1e-6)
  119.   )
  120.  
  121.   (defun unique ( pl )
  122.     (if pl
  123.       (cons (car pl)
  124.             (unique
  125.               (vl-remove-if
  126.                 '(lambda ( x )
  127.                    (equal x (car pl) 1e-6)
  128.                  )
  129.                 (cdr pl)
  130.               )
  131.             )
  132.       )
  133.     )
  134.   )
  135.  
  136.   (defun chkcircinside ( pp tll / d ci ipp ippl tst )
  137.     (if (and pp tll)
  138.       (progn
  139.         (setq ci (entmakex (list (cons 0 "CIRCLE") (cons 10 pp) (cons 40 (offd (function <) pp tll)))))
  140.         (if (setq ipp (vlax-invoke (vlax-ename->vla-object ci) 'intersectwith (vlax-ename->vla-object ent) acextendnone))
  141.           (progn
  142.             (while ipp
  143.               (setq ippl (cons (list (car ipp) (cadr ipp) (caddr ipp)) ippl))
  144.               (setq ipp (cdddr ipp))
  145.             )
  146.             (if (or (not ippl) (vl-every '(lambda ( x ) (inside-p x ent enti)) (apply 'append (mapcar '(lambda ( x ) (list (vlax-curve-getpointatparam ci (- x 0.1)) (vlax-curve-getpointatparam ci (+ x 0.1)))) (mapcar '(lambda ( x ) (vlax-curve-getparamatpoint ci x)) ippl)))))
  147.               (setq tst t)
  148.             )
  149.           )
  150.           (setq tst t)
  151.         )
  152.         (if (and ci (not (vlax-erased-p ci)))
  153.           (entdel ci)
  154.         )
  155.       )
  156.     )
  157.     tst
  158.   )
  159.  
  160.   (defun mc ( p lw / ci pl mp )
  161.     (setq ci (entmakex (list (cons 0 "CIRCLE") (cons 10 p) (cons 40 1.0))))
  162.     (setq pl (vlax-invoke (vlax-ename->vla-object ci) 'intersectwith (vlax-ename->vla-object lw) acextendnone))
  163.     (if (and ci (not (vlax-erased-p ci)))
  164.       (entdel ci)
  165.     )
  166.     (setq mp (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) (list (car pl) (cadr pl)) (list (nth 3 pl) (nth 4 pl))))
  167.     (list p mp)
  168.   )
  169.  
  170.   (defun preprocess1 ( vl vli / ip ) ;;; iplst - lexical global variable
  171.     (setq iplst (mapcar '(lambda ( p1 p2 p3 p4 / ip )
  172.                           (if
  173.                             (and
  174.                               (setq ip (inters p1 p2 p3 p4 nil))
  175.                               (inside-p ip ent enti)
  176.                               (equal (unit (mapcar '- ip p1)) (unit (mapcar '- p2 p1)) 1e-6)
  177.                               (equal (unit (mapcar '- ip p3)) (unit (mapcar '- p4 p3)) 1e-6)
  178.                               (inside-p (mapcar '+ p1 (mapcar '* (unit (mapcar '- ip p1)) (list 1e-2 1e-2))) ent enti)
  179.                               (inside-p (mapcar '+ p3 (mapcar '* (unit (mapcar '- ip p3)) (list 1e-2 1e-2))) ent enti)
  180.                             )
  181.                             (list ip (list p1 p3))
  182.                             (list nil nil)
  183.                           )
  184.                         )
  185.                         vl
  186.                         vli
  187.                         (append (cdr vl) (list (car vl)))
  188.                         (append (cdr vli) (list (car vli)))
  189.                 )
  190.     )
  191.     iplst
  192.   )
  193.  
  194.   (defun preprocess2 ( vl vli / ip ) ;;; iplst - lexical global variable
  195.     (setq iplst (mapcar '(lambda ( p1 p2 p3 p4 / ip )
  196.                           (if
  197.                             (and
  198.                               (setq ip (inters p1 p2 p3 p4 nil))
  199.                               (inside-p ip ent enti)
  200.                               (equal (unit (mapcar '- ip p1)) (unit (mapcar '- p2 p1)) 1e-6)
  201.                               (equal (unit (mapcar '- ip p3)) (unit (mapcar '- p4 p3)) 1e-6)
  202.                               (inside-p (mapcar '+ p1 (mapcar '* (unit (mapcar '- ip p1)) (list 1e-2 1e-2))) ent enti)
  203.                               (inside-p (mapcar '+ p3 (mapcar '* (unit (mapcar '- ip p3)) (list 1e-2 1e-2))) ent enti)
  204.                             )
  205.                             (list ip (list p1 p3))
  206.                             (list nil nil)
  207.                           )
  208.                         )
  209.                         vl
  210.                         vli
  211.                         (mapcar '(lambda ( x ) (car (mc (caar lil) (car lwnl)))) vl)
  212.                         (mapcar '(lambda ( x ) (cadr (mc (caar lil) (car lwnl)))) vli)
  213.                 )
  214.     )
  215.     iplst
  216.   )
  217.  
  218.   (defun rem-vllvlli ( lil lwnl )
  219.     (if (vl-some '(lambda ( x ) (equal x (cadr (car lil)) 1e-2)) vll)
  220.       (progn
  221.         (setq vll (vl-remove-if '(lambda ( x ) (equal (cadr (car lil)) x 1e-2)) vll))
  222.         (setq vlli (vl-remove-if '(lambda ( x ) (equal (cadr (car lil)) x 1e-2)) vlli))
  223.       )
  224.     )
  225.     (if (vl-some '(lambda ( x ) (equal x (cadr (cadr lil)) 1e-2)) vll)
  226.       (progn
  227.         (setq vll (vl-remove-if '(lambda ( x ) (equal (cadr (cadr lil)) x 1e-2)) vll))
  228.         (setq vlli (vl-remove-if '(lambda ( x ) (equal (cadr (cadr lil)) x 1e-2)) vlli))
  229.       )
  230.     )
  231.   )
  232.  
  233.   (defun subprocess ( iplst / p1 p2 pp1 pp2 )
  234.     (setq iplst (vl-remove-if '(lambda ( x ) (or (null (car x)) (null (cadr x)))) iplst))
  235.     (setq iplstoffd (mapcar '(lambda ( x ) (if (and (car x) (cadr x))
  236.                                              (list
  237.                                                (mapcar '+ '(0.0 0.0) (car x))
  238.                                                (mapcar '(lambda ( y ) (mapcar '+ '(0.0 0.0) y)) (cadr x))
  239.                                                (offd (function <) (mapcar '+ '(0.0 0.0) (car x)) tlll)
  240.                                              )
  241.                                              (list nil nil nil)
  242.                                            )
  243.                              ) iplst
  244.                     )
  245.     )
  246.     (setq iplstoffd (vl-remove-if '(lambda ( x ) (null (caddr x))) iplstoffd))
  247.     (setq iplstoffd (vl-sort iplstoffd '(lambda ( a b ) (< (caddr a) (caddr b)))))
  248.     (if (not (vl-position (car iplstoffd) lst))
  249.       (setq ipd (car iplstoffd) lst (cons ipd lst))
  250.       (progn
  251.         (setq iplstoffd (cdr (member (car lst) iplstoffd)))
  252.         (setq ipd (car iplstoffd) lst (cons ipd lst))
  253.       )
  254.     )
  255.     (if (and ipd (chkcircinside (car ipd) tlll))
  256.       (progn
  257.         (setq ppp (cons (car ipd) ppp))
  258.         (if
  259.           (and
  260.             (setq p1
  261.               (vl-some '(lambda ( x )
  262.                 (if
  263.                   (equal
  264.                     (unit (mapcar '- (car ipd) x))
  265.                     (unit (mapcar '- (car ipd) (caadr ipd)))
  266.                     1e-6
  267.                   )
  268.                   x
  269.                 )
  270.               ) vll
  271.               )
  272.             )
  273.             (setq pp1
  274.               (vl-some '(lambda ( x )
  275.                 (if
  276.                   (equal
  277.                     (unit (mapcar '- (car ipd) x))
  278.                     (unit (mapcar '- (car ipd) p1))
  279.                     1e-6
  280.                   )
  281.                   x
  282.                 )
  283.               ) vlll
  284.               )
  285.             )
  286.           )
  287.           (setq lil (cons (list (car ipd) pp1) lil))
  288.           (if p1
  289.             (setq lil (cons (list (car ipd) p1) lil))
  290.             (setq lil (cons (list (car ipd) (caadr ipd)) lil))
  291.           )
  292.         )
  293.         (if
  294.           (and
  295.             (setq p2
  296.               (vl-some '(lambda ( x )
  297.                 (if
  298.                   (equal
  299.                     (unit (mapcar '- (car ipd) x))
  300.                     (unit (mapcar '- (car ipd) (cadadr ipd)))
  301.                     1e-6
  302.                   )
  303.                   x
  304.                 )
  305.               ) vll
  306.               )
  307.             )
  308.             (setq pp2
  309.               (vl-some '(lambda ( x )
  310.                 (if
  311.                   (equal
  312.                     (unit (mapcar '- (car ipd) x))
  313.                     (unit (mapcar '- (car ipd) p2))
  314.                     1e-6
  315.                   )
  316.                   x
  317.                 )
  318.               ) vlll
  319.               )
  320.             )
  321.           )
  322.           (setq lil (cons (list (car ipd) pp2) lil))
  323.           (if p2
  324.             (setq lil (cons (list (car ipd) p2) lil))
  325.             (setq lil (cons (list (car ipd) (cadadr ipd)) lil))
  326.           )
  327.         )
  328.         (if
  329.           (and
  330.             (caddr ipd)
  331.             (setq lwnl (vl-catch-all-apply
  332.               'vlax-invoke
  333.               (list (vlax-ename->vla-object lw) 'offset (- (caddr ipd)))
  334.             ))
  335.           )
  336.           (if (and lwnl (not (vl-catch-all-error-p lwnl)))
  337.             (setq lwnl (mapcar 'vlax-vla-object->ename lwnl))
  338.             (setq done t)
  339.           )
  340.         )
  341.         (cond
  342.           ( (and lwnl (not (vl-catch-all-error-p lwnl)) (= (length lwnl) 1))
  343.             (rem-vllvlli lil lwnl)
  344.           )
  345.           ( (and lwnl (not (vl-catch-all-error-p lwnl)) (> (length lwnl) 1))
  346.             (setq lwnl (vl-sort lwnl '(lambda ( a b ) (< (distance (car ipd) (vlax-curve-getclosestpointto a (car ipd))) (distance (car ipd) (vlax-curve-getclosestpointto b (car ipd)))))))
  347.             (rem-vllvlli lil lwnl)
  348.           )
  349.         )
  350.       )
  351.     )
  352.     lwnl
  353.   )
  354.  
  355.   (defun process ( lw / lwi lwx vl vli iplst tl lwnl ) ;;; vll, vlli, tll, lwnl, ppp, done - lexical global variables ;;;
  356.     (if lw
  357.       (progn
  358.         (setq vl (mapcar 'cdr (vl-remove-if '(lambda ( x ) (/= (car x) 10)) (setq lwx (entget lw)))))
  359.         (if (not vll)
  360.           (setq vll vl)
  361.         )
  362.         (setq lwi (vlax-vla-object->ename (car (vlax-invoke (vlax-ename->vla-object lw) 'offset -1e-3))))
  363.         (setq vli (mapcar 'cdr (vl-remove-if '(lambda ( x ) (/= (car x) 10)) (entget lwi))))
  364.         (if (not vlli)
  365.           (setq vlli vli)
  366.         )
  367.         (setq tl (mapcar '(lambda ( a b ) (list a b)) vl (append (cdr vl) (list (car vl)))))
  368.         (if (not tll)
  369.           (setq tll tl)
  370.         )
  371.         (repeat (* 5 (length vll))
  372.           (setq iplst nil)
  373.           (setq iplst (preprocess1 vll vlli))
  374.           (subprocess iplst)
  375.           (setq iplst nil)
  376.           (setq iplst (preprocess2 vll vlli))
  377.           (subprocess iplst)
  378.         )
  379.         (if (and lwi (not (vlax-erased-p lwi)))
  380.           (entdel lwi)
  381.         )
  382.         lwnl
  383.       )
  384.     )
  385.   )
  386.  
  387.   (or doc (setq doc (vla-get-activedocument cad)))
  388.   (or spc (setq spc (vla-get-block (setq lay (vla-get-activelayout doc)))))
  389.  
  390.   (if (= 8 (logand 8 (getvar 'undoctl)))
  391.     (vla-endundomark doc)
  392.   )
  393.   (if
  394.     (and
  395.       (setq lw (car (entsel "\nPick boundary closed polygonal LWPOLYLINE with only straight segments...")))
  396.       (= (cdr (assoc 0 (setq lwx (entget lw)))) "LWPOLYLINE")
  397.       (= 1 (logand 1 (cdr (assoc 70 lwx))))
  398.       (vl-every '(lambda ( x ) (= 0.0 (cdr x))) (vl-remove-if '(lambda ( x ) (/= (car x) 42)) lwx))
  399.       (setq el (entlast) ent lw)
  400.     )
  401.     (progn
  402.       (setq lwi (vlax-vla-object->ename (car (vlax-invoke (vlax-ename->vla-object lw) 'offset -1e-3))))
  403.       (if (> (vlax-curve-getarea lwi) (vlax-curve-getarea lw))
  404.         (setq lw (rlw lw)) ;;; force main lwpolyline CCW - counter clockwise ;;;
  405.       )
  406.       (setq enti (vlax-vla-object->ename (car (vlax-invoke (vlax-ename->vla-object ent) 'offset -1e-3))))
  407.       (if (and lwi (not (vlax-erased-p lwi)))
  408.         (entdel lwi)
  409.       )
  410.       (if (not f)
  411.         (progn
  412.           (setq vlll (mapcar 'cdr (vl-remove-if '(lambda ( x ) (/= (car x) 10)) (entget ent))))
  413.           (setq vllli (mapcar 'cdr (vl-remove-if '(lambda ( x ) (/= (car x) 10)) (entget enti))))
  414.           (setq tlll (mapcar '(lambda ( a b ) (list a b)) vlll (append (cdr vlll) (list (car vlll)))))
  415.           (setq f t)
  416.         )
  417.       )
  418.       (initget "Yes No")
  419.       (setq ch (cond ( (getkword "\nDo you want points-offsets-lines or just lines [Yes / No] <Yes> : ") ) ( "Yes" )))
  420.       (while (not done)
  421.         (if
  422.           (and
  423.             (not (vl-catch-all-error-p (setq lwnl (vl-catch-all-apply 'process (list lw)))))
  424.             lwnl
  425.           )
  426.           (while (and (not done) (setq lww (car (vl-sort lwnl '(lambda ( a b ) (> (vlax-curve-getarea a) (vlax-curve-getarea b)))))))
  427.             (setq lwnl (vl-remove lww lwnl))
  428.             (setq lwo lww)
  429.             (setq lwnl (vl-catch-all-apply 'process (list lww)))
  430.             (if (vl-catch-all-error-p lwnl)
  431.               (setq done t)
  432.               (if (eq (car lwnl) lwo)
  433.                 (setq done t)
  434.               )
  435.             )
  436.           )
  437.           (setq done t)
  438.         )
  439.       ) ;;; main engine ;;;
  440.     )
  441.   )
  442.   (*error* nil)
  443. )
  444.  

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 10, 2023, 02:47:02 AM
Still not good, but logic is correct, so it should work now better...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:roof-new-new-new ( / *error* mid _offset unit rlw offd inside-p collinear-p unique chkcircinside mc preprocess1 preprocess2 rem-vllvlli subprocess process done lw lwi lwx lwo ent enti lil lix lwnl ch el p1 p2 pp ppp ipp ippl vll vlli tll iplst lst ) ;;; cad, doc, spc, lay, vlll, vllli, tlll, f - global variables ;;;
  2.  
  3.  
  4.   (defun *error* ( m )
  5.     (if (and lwi (not (vlax-erased-p lwi)))
  6.       (entdel lwi)
  7.     )
  8.     (if (and enti (not (vlax-erased-p enti)))
  9.       (entdel enti)
  10.     )
  11.     (if (setq ppp (unique ppp))
  12.       (foreach pp ppp
  13.         (if (not (vl-some '(lambda ( x ) (and (not (equal (car x) pp 1e-6)) (not (equal (cadr x) pp 1e-6)) (not (equal (car x) (cadr x) 1e-6)) (collinear-p (car x) pp (cadr x)))) lil))
  14.           (entmake (list (cons 0 "POINT") (cons 10 pp) (cons 62 1)))
  15.         )
  16.       )
  17.     )
  18.     (if (= ch "No")
  19.       (while (setq el (entnext el))
  20.         (if (and el (not (vlax-erased-p el)))
  21.           (entdel el)
  22.         )
  23.       )
  24.     )
  25.     (if lil
  26.       (foreach li lil
  27.         (if (vl-some '(lambda ( x ) (and (not (equal (car li) x 1e-6)) (not (equal (cadr li) x 1e-6)) (not (equal (car li) (cadr li) 1e-6)) (collinear-p (car li) x (cadr li)))) ppp)
  28.           (setq lil (vl-remove li lil))
  29.           (if (vl-some '(lambda ( x ) (equal x (car li) 1e-6)) ppp)
  30.             (entmake (list (cons 0 "LINE") (cons 10 (car li)) (cons 11 (cadr li)) (cons 62 3)))
  31.           )
  32.         )
  33.       )
  34.     )
  35.     (command-s "_.-OVERKILL" "_ALL" "_T" "_Y" "" "")
  36.     (if (= 8 (logand 8 (getvar 'undoctl)))
  37.       (vla-endundomark doc)
  38.     )
  39.     (prompt "\nIf you want to start again on different sample, make sure you nil flag f : (setq f nil vlll nil vllli nil tlll nil) ...")
  40.     (if m
  41.       (prompt m)
  42.     )
  43.     (princ)
  44.   )
  45.  
  46.   (defun mid ( p1 p2 )
  47.     (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) p1 p2)
  48.   )
  49.  
  50.   (defun _offset ( lw dist / lill lww vl lwx tl tln iplst lws )
  51.     (setq vl (mapcar 'cdr (vl-remove-if '(lambda ( x ) (/= (car x) 10)) (setq lwx (entget lw)))))
  52.     (setq tl (mapcar '(lambda ( a b ) (list a b)) vl (append (cdr vl) (list (car vl)))))
  53.     (setq tln (mapcar '(lambda ( x ) (list (polar (car x) (+ (* 0.5 pi) (angle (car x) (cadr x))) (- dist)) (polar (cadr x) (+ (* 0.5 pi) (angle (car x) (cadr x))) (- dist)))) tl))
  54.     (setq iplst (mapcar '(lambda ( a b ) (inters (car a) (cadr a) (car b) (cadr b) nil)) tln (append (cdr tln) (list (car tln)))))
  55.     (setq lill (mapcar '(lambda ( a b ) (list a b)) iplst (append (cdr iplst) (list (car iplst)))))
  56.     (setq lill (vl-remove-if '(lambda ( x ) (or (null (car x)) (null (cadr x)))) lill))
  57.     (setq lws
  58.       (cons
  59.         (setq lww
  60.           (entmakex
  61.             (append
  62.               (list
  63.                 (cons 0 "LWPOLYLINE")
  64.                 (cons 100 "AcDbEntity")
  65.                 (cons 100 "AcDbPolyline")
  66.                 (cons 90 (length iplst))
  67.                 (cons 70 (1+ (* 128 (getvar 'plinegen))))
  68.                 (cons 38 0.0)
  69.               )
  70.               (mapcar '(lambda ( p ) (cons 10 p)) iplst)
  71.               (list (list 210 0.0 0.0 1.0))
  72.             )
  73.           )
  74.         )
  75.         lws
  76.       )
  77.     )
  78.   )
  79.  
  80.   (defun unit ( v / d )
  81.     (if (not (equal (setq d (distance '(0.0 0.0 0.0) v)) 0.0 1e-6))
  82.       (mapcar '(lambda ( x ) (/ x d)) v)
  83.     )
  84.   )
  85.  
  86.   (defun rlw ( lw / e x1 x2 x3 x4 x5 x6 )
  87.     ;; by ElpanovEvgeniy
  88.     (if (= (cdr (assoc 0 (setq e (entget lw)))) "LWPOLYLINE")
  89.       (progn
  90.         (foreach a1 e
  91.           (cond ((= (car a1) 10) (setq x2 (cons a1 x2)))
  92.                 ((= (car a1) 40) (setq x4 (cons (cons 41 (cdr a1)) x4)))
  93.                 ((= (car a1) 41) (setq x3 (cons (cons 40 (cdr a1)) x3)))
  94.                 ((= (car a1) 42) (setq x5 (cons (cons 42 (- (cdr a1))) x5)))
  95.                 ((= (car a1) 210) (setq x6 (cons a1 x6)))
  96.                 (t (setq x1 (cons a1 x1)))
  97.           )
  98.         )
  99.         (entmod (append (reverse x1)
  100.                   (append (apply 'append
  101.                             (apply 'mapcar
  102.                               (cons 'list
  103.                                 (list x2
  104.                                   (cdr (reverse (cons (car x3) (reverse x3))))
  105.                                   (cdr (reverse (cons (car x4) (reverse x4))))
  106.                                   (cdr (reverse (cons (car x5) (reverse x5))))
  107.                                 )
  108.                               )
  109.                             )
  110.                           )
  111.                           x6
  112.                   )
  113.                 )
  114.         )
  115.         (entupd lw)
  116.       )
  117.     )
  118.   )
  119.  
  120.   (defun offd ( sign ip tl / dl )
  121.     (setq dl (mapcar '(lambda ( x )
  122.                         (distance ip
  123.                           (inters
  124.                             ip
  125.                             (polar ip (+ (* 0.5 pi) (angle (car x) (cadr x))) 1.0)
  126.                             (car x)
  127.                             (polar (car x) (angle (car x) (cadr x)) 1.0)
  128.                             nil
  129.                           )
  130.                         )
  131.                       ) tl
  132.               )
  133.     )
  134.     (vl-some '(lambda ( x )
  135.                (if
  136.                  (>
  137.                    (-
  138.                      (length dl)
  139.                      (length (vl-remove-if '(lambda ( y ) (equal x y 0.05)) dl))
  140.                    ) 2
  141.                  )
  142.                  x
  143.                )
  144.              ) (vl-sort dl sign)
  145.     )
  146.   )
  147.  
  148.   (defun inside-p ( p lw lwi )
  149.   )
  150.  
  151.   (defun collinear-p ( p1 p p2 )
  152.     (equal (distance p1 p2) (+ (distance p1 p) (distance p p2)) 1e-6)
  153.   )
  154.  
  155.   (defun unique ( pl )
  156.     (if pl
  157.       (cons (car pl)
  158.             (unique
  159.               (vl-remove-if
  160.                 '(lambda ( x )
  161.                    (equal x (car pl) 1e-6)
  162.                  )
  163.                 (cdr pl)
  164.               )
  165.             )
  166.       )
  167.     )
  168.   )
  169.  
  170.   (defun chkcircinside ( pp tll / d ci ipp ippl tst )
  171.     (if (and pp tll)
  172.       (progn
  173.         (setq ci (entmakex (list (cons 0 "CIRCLE") (cons 10 pp) (cons 40 (offd (function <) pp tll)))))
  174.         (if (setq ipp (vlax-invoke (vlax-ename->vla-object ci) 'intersectwith (vlax-ename->vla-object ent) acextendnone))
  175.           (progn
  176.             (while ipp
  177.               (setq ippl (cons (list (car ipp) (cadr ipp) (caddr ipp)) ippl))
  178.               (setq ipp (cdddr ipp))
  179.             )
  180.             (if (or (not ippl) (vl-every '(lambda ( x ) (inside-p x ent enti)) (apply 'append (mapcar '(lambda ( x ) (list (vlax-curve-getpointatparam ci (- x 0.1)) (vlax-curve-getpointatparam ci (+ x 0.1)))) (mapcar '(lambda ( x ) (vlax-curve-getparamatpoint ci x)) ippl)))))
  181.               (setq tst t)
  182.             )
  183.           )
  184.           (setq tst t)
  185.         )
  186.         (if (and ci (not (vlax-erased-p ci)))
  187.           (entdel ci)
  188.         )
  189.       )
  190.     )
  191.     tst
  192.   )
  193.  
  194.   (defun mc ( p lw / ci pl mp p1 p2 p3 p4 )
  195.     (setq ci (entmakex (list (cons 0 "CIRCLE") (cons 10 p) (cons 40 2.0))))
  196.     (setq pl (vlax-invoke (vlax-ename->vla-object ci) 'intersectwith (vlax-ename->vla-object lw) acextendnone))
  197.     (if (and ci (not (vlax-erased-p ci)))
  198.       (entdel ci)
  199.     )
  200.     (cond
  201.       ( (= (length pl) 12)
  202.         (setq p1 (list (car pl) (cadr pl)))
  203.         (setq p2 (list (nth 3 pl) (nth 4 pl)))
  204.         (setq p3 (list (nth 6 pl) (nth 7 pl)))
  205.         (setq p4 (list (nth 9 pl) (nth 10 pl)))
  206.         (cond
  207.           ( (vl-some '(lambda ( li ) (or (equal (unit (mapcar '- (car li) (cadr li))) (unit (mapcar '- (mid p1 p2) p)) 1e-3) (equal (unit (mapcar '- (car li) (cadr li))) (unit (mapcar '- p (mid p2 p1))) 1e-3))) lil)
  208.             (setq mp (mid p3 p4))
  209.           )
  210.           ( (vl-some '(lambda ( li ) (or (equal (unit (mapcar '- (car li) (cadr li))) (unit (mapcar '- (mid p2 p3) p)) 1e-3) (equal (unit (mapcar '- (car li) (cadr li))) (unit (mapcar '- p (mid p3 p2))) 1e-3))) lil)
  211.             (setq mp (mid p4 p1))
  212.           )
  213.           ( (vl-some '(lambda ( li ) (or (equal (unit (mapcar '- (car li) (cadr li))) (unit (mapcar '- (mid p3 p4) p)) 1e-3) (equal (unit (mapcar '- (car li) (cadr li))) (unit (mapcar '- p (mid p4 p3))) 1e-3))) lil)
  214.             (setq mp (mid p1 p2))
  215.           )
  216.           ( (vl-some '(lambda ( li ) (or (equal (unit (mapcar '- (car li) (cadr li))) (unit (mapcar '- (mid p4 p1) p)) 1e-3) (equal (unit (mapcar '- (car li) (cadr li))) (unit (mapcar '- p (mid p1 p4))) 1e-3))) lil)
  217.             (setq mp (mid p2 p3))
  218.           )
  219.         )
  220.       )
  221.       ( (= (length pl) 6)
  222.         (setq p1 (list (car pl) (cadr pl)))
  223.         (setq p2 (list (nth 3 pl) (nth 4 pl)))
  224.         (setq mp (mid p1 p2))
  225.       )
  226.     )
  227.     (list p mp)
  228.   )
  229.  
  230.   (defun preprocess1 ( vl vli / ip ) ;;; iplst - lexical global variable
  231.     (setq iplst (mapcar '(lambda ( p1 p2 p3 p4 / ip )
  232.                           (if
  233.                             (and
  234.                               (setq ip (inters p1 p2 p3 p4 nil))
  235.                               (inside-p ip ent enti)
  236.                               (equal (unit (mapcar '- ip p1)) (unit (mapcar '- p2 p1)) 1e-6)
  237.                               (equal (unit (mapcar '- ip p3)) (unit (mapcar '- p4 p3)) 1e-6)
  238.                               (inside-p (mapcar '+ p1 (mapcar '* (unit (mapcar '- ip p1)) (list 1e-2 1e-2))) ent enti)
  239.                               (inside-p (mapcar '+ p3 (mapcar '* (unit (mapcar '- ip p3)) (list 1e-2 1e-2))) ent enti)
  240.                             )
  241.                             (list ip (list p1 p3))
  242.                             (list nil nil)
  243.                           )
  244.                         )
  245.                         vl
  246.                         vli
  247.                         (append (cdr vl) (list (car vl)))
  248.                         (append (cdr vli) (list (car vli)))
  249.                 )
  250.     )
  251.     iplst
  252.   )
  253.  
  254.   (defun preprocess2 ( vl vli / ip ) ;;; iplst - lexical global variable
  255.     (setq iplst (mapcar '(lambda ( p1 p2 p3 p4 / ip )
  256.                           (if
  257.                             (and
  258.                               (setq ip (inters p1 p2 p3 p4 nil))
  259.                               (inside-p ip ent enti)
  260.                               (equal (unit (mapcar '- ip p1)) (unit (mapcar '- p2 p1)) 1e-6)
  261.                               (equal (unit (mapcar '- ip p3)) (unit (mapcar '- p4 p3)) 1e-6)
  262.                               (inside-p (mapcar '+ p1 (mapcar '* (unit (mapcar '- ip p1)) (list 1e-2 1e-2))) ent enti)
  263.                               (inside-p (mapcar '+ p3 (mapcar '* (unit (mapcar '- ip p3)) (list 1e-2 1e-2))) ent enti)
  264.                             )
  265.                             (list ip (list p1 p3))
  266.                             (list nil nil)
  267.                           )
  268.                         )
  269.                         vl
  270.                         vli
  271.                         (mapcar '(lambda ( x ) (car (mc (caar lil) (cond ( (ssname (ssget "_C" (caar lil) (caar lil) '((0 . "LWPOLYLINE"))) 0) ) ( entlast ))))) vl)
  272.                         (mapcar '(lambda ( x ) (cadr (mc (caar lil) (cond ( (ssname (ssget "_C" (caar lil) (caar lil) '((0 . "LWPOLYLINE"))) 0) ) ( entlast ))))) vli)
  273.                 )
  274.     )
  275.     iplst
  276.   )
  277.  
  278.   (defun rem-vllvlli ( lil )
  279.     (if (vl-some '(lambda ( x ) (equal x (cadr (car lil)) 1e-2)) vll)
  280.       (progn
  281.         (setq vll (vl-remove-if '(lambda ( x ) (equal (cadr (car lil)) x 1e-2)) vll))
  282.         (setq vlli (vl-remove-if '(lambda ( x ) (equal (cadr (car lil)) x 1e-2)) vlli))
  283.       )
  284.     )
  285.     (if (vl-some '(lambda ( x ) (equal x (cadr (cadr lil)) 1e-2)) vll)
  286.       (progn
  287.         (setq vll (vl-remove-if '(lambda ( x ) (equal (cadr (cadr lil)) x 1e-2)) vll))
  288.         (setq vlli (vl-remove-if '(lambda ( x ) (equal (cadr (cadr lil)) x 1e-2)) vlli))
  289.       )
  290.     )
  291.   )
  292.  
  293.   (defun subprocess ( iplst flag / p1 p2 pp1 pp2 )
  294.     (setq iplst (vl-remove-if '(lambda ( x ) (or (null (car x)) (null (cadr x)))) iplst))
  295.     (setq iplstoffd (mapcar '(lambda ( x ) (if (and (car x) (cadr x))
  296.                                              (list
  297.                                                (mapcar '+ '(0.0 0.0) (car x))
  298.                                                (mapcar '(lambda ( y ) (mapcar '+ '(0.0 0.0) y)) (cadr x))
  299.                                                (offd (function <) (mapcar '+ '(0.0 0.0) (car x)) tlll)
  300.                                              )
  301.                                              (list nil nil nil)
  302.                                            )
  303.                              ) iplst
  304.                     )
  305.     )
  306.     (setq iplstoffd (vl-remove-if '(lambda ( x ) (null (caddr x))) iplstoffd))
  307.     (if (not flag)
  308.       (setq iplstoffd (vl-sort iplstoffd '(lambda ( a b ) (< (caddr a) (caddr b)))))
  309.       (setq iplstoffd (vl-sort iplstoffd '(lambda ( a b ) (if (equal (caddr a) (caddr b) 1e-6) (< (distance (car a) (caar lst)) (distance (car b) (caar lst))) (< (caddr a) (caddr b))))))
  310.     )
  311.     (if (not (vl-position (car iplstoffd) lst))
  312.       (setq ipd (car iplstoffd) lst (cons ipd lst))
  313.       (progn
  314.         (setq iplstoffd (cdr (member (car lst) iplstoffd)))
  315.         (setq ipd (car iplstoffd) lst (cons ipd lst))
  316.       )
  317.     )
  318.     (if (and ipd (chkcircinside (car ipd) tlll))
  319.       (progn
  320.         (setq ppp (cons (car ipd) ppp))
  321.         (if
  322.           (and
  323.             (setq p1
  324.               (vl-some '(lambda ( x )
  325.                 (if
  326.                   (equal
  327.                     (unit (mapcar '- (car ipd) x))
  328.                     (unit (mapcar '- (car ipd) (caadr ipd)))
  329.                     1e-6
  330.                   )
  331.                   x
  332.                 )
  333.               ) vll
  334.               )
  335.             )
  336.             (setq pp1
  337.               (vl-some '(lambda ( x )
  338.                 (if
  339.                   (equal
  340.                     (unit (mapcar '- (car ipd) x))
  341.                     (unit (mapcar '- (car ipd) p1))
  342.                     1e-6
  343.                   )
  344.                   x
  345.                 )
  346.               ) vlll
  347.               )
  348.             )
  349.           )
  350.           (setq lil (cons (list (car ipd) pp1) lil))
  351.           (if p1
  352.             (setq lil (cons (list (car ipd) p1) lil))
  353.             (setq lil (cons (list (car ipd) (caadr ipd)) lil))
  354.           )
  355.         )
  356.         (if
  357.           (and
  358.             (setq p2
  359.               (vl-some '(lambda ( x )
  360.                 (if
  361.                   (equal
  362.                     (unit (mapcar '- (car ipd) x))
  363.                     (unit (mapcar '- (car ipd) (cadadr ipd)))
  364.                     1e-6
  365.                   )
  366.                   x
  367.                 )
  368.               ) vll
  369.               )
  370.             )
  371.             (setq pp2
  372.               (vl-some '(lambda ( x )
  373.                 (if
  374.                   (equal
  375.                     (unit (mapcar '- (car ipd) x))
  376.                     (unit (mapcar '- (car ipd) p2))
  377.                     1e-6
  378.                   )
  379.                   x
  380.                 )
  381.               ) vlll
  382.               )
  383.             )
  384.           )
  385.           (setq lil (cons (list (car ipd) pp2) lil))
  386.           (if p2
  387.             (setq lil (cons (list (car ipd) p2) lil))
  388.             (setq lil (cons (list (car ipd) (cadadr ipd)) lil))
  389.           )
  390.         )
  391.         (if
  392.           (and
  393.             (caddr ipd)
  394.             (setq lwnl (vl-catch-all-apply '_offset (list ent (- (caddr ipd)))))
  395.           )
  396.           (if (and lwnl (vl-catch-all-error-p lwnl))
  397.             (setq done t)
  398.           )
  399.         )
  400.         (cond
  401.           ( (and lwnl (not (vl-catch-all-error-p lwnl)) (= (length lwnl) 1))
  402.             (rem-vllvlli lil)
  403.           )
  404.           ( (and lwnl (not (vl-catch-all-error-p lwnl)) (> (length lwnl) 1))
  405.             (setq lwnl (vl-sort lwnl '(lambda ( a b ) (< (distance (car ipd) (vlax-curve-getclosestpointto a (car ipd))) (distance (car ipd) (vlax-curve-getclosestpointto b (car ipd)))))))
  406.             (rem-vllvlli lil)
  407.           )
  408.         )
  409.       )
  410.     )
  411.     lwnl
  412.   )
  413.  
  414.   (defun process ( lw / lwi lwx vl vli iplst tl lwnl ) ;;; vll, vlli, tll, lwnl, ppp, done, flag - lexical global variables ;;;
  415.     (if lw
  416.       (progn
  417.         (setq vl (mapcar 'cdr (vl-remove-if '(lambda ( x ) (/= (car x) 10)) (setq lwx (entget lw)))))
  418.         (if (not vll)
  419.           (setq vll vl)
  420.         )
  421.         (setq lwi (vlax-vla-object->ename (car (vlax-invoke (vlax-ename->vla-object lw) 'offset -1e-3))))
  422.         (setq vli (mapcar 'cdr (vl-remove-if '(lambda ( x ) (/= (car x) 10)) (entget lwi))))
  423.         (if (not vlli)
  424.           (setq vlli vli)
  425.         )
  426.         (setq tl (mapcar '(lambda ( a b ) (list a b)) vl (append (cdr vl) (list (car vl)))))
  427.         (if (not tll)
  428.           (setq tll tl)
  429.         )
  430.         (setq iplst nil)
  431.         (setq iplst (preprocess1 vll vlli))
  432.         (subprocess iplst nil)
  433.         (repeat (length vll)
  434.           (setq iplst nil)
  435.           (setq iplst (preprocess2 vll vlli))
  436.           (subprocess iplst t)
  437.         )
  438.         (if (and lwi (not (vlax-erased-p lwi)))
  439.           (entdel lwi)
  440.         )
  441.         lwnl
  442.       )
  443.     )
  444.   )
  445.  
  446.   (or doc (setq doc (vla-get-activedocument cad)))
  447.   (or spc (setq spc (vla-get-block (setq lay (vla-get-activelayout doc)))))
  448.  
  449.   (if (= 8 (logand 8 (getvar 'undoctl)))
  450.     (vla-endundomark doc)
  451.   )
  452.   (if
  453.     (and
  454.       (setq lw (car (entsel "\nPick boundary closed polygonal LWPOLYLINE with only straight segments...")))
  455.       (= (cdr (assoc 0 (setq lwx (entget lw)))) "LWPOLYLINE")
  456.       (= 1 (logand 1 (cdr (assoc 70 lwx))))
  457.       (vl-every '(lambda ( x ) (= 0.0 (cdr x))) (vl-remove-if '(lambda ( x ) (/= (car x) 42)) lwx))
  458.       (setq el (entlast) ent lw)
  459.     )
  460.     (progn
  461.       (setq lwi (vlax-vla-object->ename (car (vlax-invoke (vlax-ename->vla-object lw) 'offset -1e-3))))
  462.       (if (> (vlax-curve-getarea lwi) (vlax-curve-getarea lw))
  463.         (setq lw (rlw lw)) ;;; force main lwpolyline CCW - counter clockwise ;;;
  464.       )
  465.       (setq enti (vlax-vla-object->ename (car (vlax-invoke (vlax-ename->vla-object ent) 'offset -1e-3))))
  466.       (if (and lwi (not (vlax-erased-p lwi)))
  467.         (entdel lwi)
  468.       )
  469.       (if (not f)
  470.         (progn
  471.           (setq vlll (mapcar 'cdr (vl-remove-if '(lambda ( x ) (/= (car x) 10)) (entget ent))))
  472.           (setq vllli (mapcar 'cdr (vl-remove-if '(lambda ( x ) (/= (car x) 10)) (entget enti))))
  473.           (setq tlll (mapcar '(lambda ( a b ) (list a b)) vlll (append (cdr vlll) (list (car vlll)))))
  474.           (setq f t)
  475.         )
  476.       )
  477.       (initget "Yes No")
  478.       (setq ch (cond ( (getkword "\nDo you want points-offsets-lines or just lines [Yes / No] <No> : ") ) ( "No" )))
  479.       (while (not done)
  480.         (if
  481.           (and
  482.             (not (vl-catch-all-error-p (setq lwnl (vl-catch-all-apply 'process (list lw)))))
  483.             lwnl
  484.           )
  485.           (while (and (not done) (setq lww (car (vl-sort lwnl '(lambda ( a b ) (> (vlax-curve-getarea a) (vlax-curve-getarea b)))))))
  486.             (setq lwnl (vl-remove lww lwnl))
  487.             (setq lwo lww)
  488.             (setq lwnl (vl-catch-all-apply 'process (list lww)))
  489.             (if (vl-catch-all-error-p lwnl)
  490.               (setq done t)
  491.               (if (eq (car lwnl) lwo)
  492.                 (setq done t)
  493.               )
  494.             )
  495.           )
  496.           (setq done t)
  497.         )
  498.       ) ;;; main engine ;;;
  499.     )
  500.   )
  501.   (*error* nil)
  502. )
  503.  

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 11, 2023, 03:26:14 AM
Who is interested in this over custom _offset sub function version, I've made few improvements, but still it doesn't solve but there is attempt which is pretty good for a start for experienced member that wants to continue... Also I've implemented *.lsp in archive at www.cadtutor.net download section...
If nothing, I've played with this challenge one more time...

Thanks for following, but I see there are no too much interest for roofs - hipped ones...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 14, 2023, 02:49:38 AM
Who is interested in this over custom _offset sub function version, I've made few improvements, but still it doesn't solve but there is attempt which is pretty good for a start for experienced member that wants to continue... Also I've implemented *.lsp in archive at www.cadtutor.net download section...
If nothing, I've played with this challenge one more time...

Thanks for following, but I see there are no too much interest for roofs - hipped ones...

Anyone?
You can download attachment from previous post and continue on that basis... We need fast and good code still... And I wanted to be used offset relations as this is correct path for correct solution...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: It's Alive! on August 14, 2023, 05:05:03 AM
Cool stuff, never had the need to draw sloped roofs. back when I was drafting, I was in New Mexico, most of the houses were Adobe, with flat roofs.
That, and I mostly did submittals for commercial casework
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 14, 2023, 05:15:54 AM
Cool stuff, never had the need to draw sloped roofs. back when I was drafting, I was in New Mexico, most of the houses were Adobe, with flat roofs.
That, and I mostly did submittals for commercial casework

Daniel, if it was you downloaded my post, I must say that solution exist in archieve on www.cadtutor.net - download section... This was for fun and for me to see if it would behave correct with offsettings... But sadly I was wrong, so here is the link at cadtutor with all neccessary routines you may find for correct construct 2d+3d solutions and there are even hatches... Main one is : 2droof-final.lsp and others based on it... But I'd check firstly : hr.lsp and if it fails, then 2droof-final.lsp... There is also good command version : roof-command-new.lsp and my versions for finding random solutions based on input correct one (2droof-final.lsp)... Those are just possibilities and solution version works very slow, so if you want improvements for speed, this could be the task for challenging *.arx, or *.dll, or ...

Link : https://www.cadtutor.net/forum/files/file/36-hipped-roof-routines/
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 14, 2023, 05:18:51 PM
I've changed (mc) sub function... Also I've changed the code all around where I founded it's necessity...

HTH., M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 18, 2023, 10:14:42 AM
I have no feedback...

Could someone have a look into my attachment in previous post and give opinion, or better implement something I forgot...
It seems that this all looks like joke, but I've spent a lot of time to collect and compose and this version... So please, help by download and look, or give opinion ab things implemented or more yet to be implemented next...
C'mon show some courage...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ronjonp on August 18, 2023, 12:13:26 PM
I have no feedback...

Could someone have a look into my attachment in previous post and give opinion, or better implement something I forgot...
It seems that this all looks like joke, but I've spent a lot of time to collect and compose and this version... So please, help by download and look, or give opinion ab things implemented or more yet to be implemented next...
C'mon show some courage...
I just tried it and it does nothing ?
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 18, 2023, 11:46:22 PM
I have no feedback...

Could someone have a look into my attachment in previous post and give opinion, or better implement something I forgot...
It seems that this all looks like joke, but I've spent a lot of time to collect and compose and this version... So please, help by download and look, or give opinion ab things implemented or more yet to be implemented next...
C'mon show some courage...
I just tried it and it does nothing ?

It's because you probably tested routine on othogonal polygonal LWPOLYLINE... Try this mod. attached here - should throw triangles on porches...
But when it's needed to continue from there to build complete solution it stops around first continuation points all around, or make not correct connections... With ultimate roof, that's the case, it makes triangles, at few places correct continuation and then not correct connections over whole area from one side to opposite side of roof... So it's doing something, but wrong...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 22, 2023, 05:21:00 AM
@Ron

I can not say for sure that attached version will solve at least something as I am facing the same thing on some examples - throws nothing!!! But I've sorted some things as you can see if you download file from previous post... I will try some more fixing if I find spare time, but I doubt it'll be revolutionary fixing... Now I and anyone can see that this is challenge topic... But I tried on some different basis to approach things - by using custom (_offset) function... Anyway thanks for looking into this and feedbacking...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 24, 2023, 08:43:41 AM
Hi, me again... I think I've connected what was the problem... I've removed 2 vvl and vvli upon sucessful connecting 2 lines, but also added vvl and vvli to apex of triangle (if it's somewhere in the beggining of calculations)... That with vvl and vvli should iterate all until there are no vvl and vvli... And there is the problem : (prout) sub catched error in it's operations... So routine either crashes before error handler output, or (while) loops into endless time... Either way I finished what should be working, but it's not... So I'll chill out for now until someone figures out and finish it all correctly...
Thanks for attention, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on August 28, 2023, 09:43:28 AM
I am stuck here with posted *.lsp in previous reply... Can someone see and solve it, or give opinion, suggestion, advice in right direction...
I've put also previous versions - last one is : roof-newest.lsp... Some new gurus, reply, please...

P.S. I doubt that someone will fix it as it has many lacks, but nevertheless, this was only for experimental purposes... Hope dies lastly, so we are waiting for valuable input...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 02, 2023, 05:46:38 AM
I didn't know that only @Ron came and commented what I posted... @Daniel is busy, but what ab @Cab, @Lee Mac, @Highflyingbird, @Evgeniy-@VovKa (if they are available for conflict situation in their countries)...
My latest intervention is roof-newest.lsp from pre-previous post... IMHO, it should bite and finish and somewhere that's the case, but there are examples where it throws forest of lines that don't have any relation to the code...
My sincerily best wishes to anyone reading and willing to help...
M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: BIGAL on September 02, 2023, 07:49:06 PM
I have been following your comments as years ago did a house package that included roofs it was 3D based so roof pitch was included. We just used a method of making each section of roof using pface. had a number of options Hip, Gable, Straight, Flat etc Unfortunately the package never took off. It was overtaken by Autocad Architecture.

Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 08, 2023, 04:14:54 PM
Can someone have a look at my (mc) sub function and complete routine for lacks... I believe that (mc) function is the key for solving as much as possible... I am out of fuel for coding, but no one want to even download "roof-newest.lsp" and look - 4 eyes are more than 2... I believe that I am close, but routine gives forest of lines in no relation to what's written in routine, but i may be wrong... So basically I am looking for help for this particular versions with custom (_offset) sub...

Thanks for reading,
Maybe at the end someone will relight my candles of hope...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 12, 2023, 10:10:39 AM
This version : https://www.theswamp.org/index.php?topic=41837.msg615983#msg615983
(roof-newest.lsp) is as much as it's possible with custom (_offset) sub... It can almost finish on some of examples, but on some throws nothing and on some throws forest of lines not really related to the code - routine... At the end I am half satisfied with this experimental version and depending on feedbacks topic will become closed... Still any conversion based on this custom (_offset) - *.dll; *.arx is IMHO not desirable, as it can give gibberish of lines, so only those roof2d*.lsp packed in ZIP on www.cadtutor.net in download section - those that gives solution - solutions are IMHO the ones that should be issue for conversion...
Thanks to all nevertheless you participated to challenge or not...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 16, 2023, 03:20:26 PM
Will someone have a look at (mc) sub function and reply with some better solution... I am out of fuel for thoughts... Link for downloading stuff is the same as I previously posted : https://www.theswamp.org/index.php?topic=41837.msg615983#msg615983
It seems that no one want to participate... Only @Ron replied and I must say - that throwing nothing is connected with shape that mimics MLINE converted to LWPOLYLINE, or crossings of mlinear shapes... But the main task of building solutions is to be applicabe on orthogonal and random shape polygonal LWPOLYLINE...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on September 18, 2023, 11:35:24 AM
I think that my (mc) sub function is half OK - it behave as should, but it's difficult to predict where "mp" point should be in some cases that are complex... Here I am posting (mc) and in attachment is complete *.lsp... I really need help of some guru - master to fix those forest gibberish lines it throws after execution... Thanks for attention, M.R.

Code - Auto/Visual Lisp: [Select]
  1.   (defun mc ( p lw / mid ci pl mp p1 p2 p3 p4 pp1 pp2 pp3 pp4 pp )
  2.  
  3.     (defun mid ( ci p1 p2 / par1 par2 mp )
  4.       (setq par1 (+ (* 2 pi) (vlax-curve-getparamatpoint ci p1)) par2 (+ (* 2 pi) (vlax-curve-getparamatpoint ci p2)))
  5.       (setq mp (mapcar '+ '(0.0 0.0) (vlax-curve-getpointatparam ci (/ (+ par1 par2) 2.0))))
  6.     )
  7.  
  8.     (if (and (setq ci (entmakex (list (cons 0 "CIRCLE") (cons 10 p) (cons 40 0.5)))) lw (not (vlax-erased-p ci)) (not (vlax-erased-p lw)))
  9.       (setq pl (vlax-invoke (vlax-ename->vla-object ci) 'intersectwith (vlax-ename->vla-object lw) acextendnone))
  10.     )
  11.     (if pl
  12.       (cond
  13.         ( (= (length pl) 12)
  14.           (setq p1 (list (car pl) (cadr pl)))
  15.           (setq p2 (list (nth 3 pl) (nth 4 pl)))
  16.           (setq p3 (list (nth 6 pl) (nth 7 pl)))
  17.           (setq p4 (list (nth 9 pl) (nth 10 pl)))
  18.           (setq pl (unique (list p1 p2 p3 p4)))
  19.           (setq pp (vl-some '(lambda ( x ) (if (= (length (vl-remove-if '(lambda ( y ) (equal x y 1e-6)) (list p1 p2 p3 p4))) 3) x)) pl))
  20.         )
  21.         ( (= (length pl) 9)
  22.           (setq p1 (list (car pl) (cadr pl)))
  23.           (setq p2 (list (nth 3 pl) (nth 4 pl)))
  24.           (setq p3 (list (nth 6 pl) (nth 7 pl)))
  25.           (setq pl (unique (list p1 p2 p3)))
  26.           (setq pp (vl-some '(lambda ( x ) (if (= (length (vl-remove-if '(lambda ( y ) (equal x y 1e-6)) (list p1 p2 p3))) 2) x)) pl))
  27.         )
  28.         ( (= (length pl) 6)
  29.           (setq p1 (list (car pl) (cadr pl)))
  30.           (setq p2 (list (nth 3 pl) (nth 4 pl)))
  31.           (setq pl (unique (list p1 p2)))
  32.         )
  33.         ( (= (length pl) 3)
  34.           (setq mp (list (car pl) (cadr pl)))
  35.         )
  36.       )
  37.     )
  38.     (if (and pl (not mp))
  39.       (cond
  40.         ( (= (length pl) 4)
  41.           (setq p1 (car pl))
  42.           (setq p2 (cadr pl))
  43.           (setq p3 (caddr pl))
  44.           (setq p4 (cadddr pl))
  45.           (mapcar 'set '(p1 p2 p3 p4) (vl-sort (list p1 p2 p3 p4) '(lambda ( a b ) (< (vlax-curve-getparamatpoint ci a) (vlax-curve-getparamatpoint ci b)))))
  46.           (setq pp1 (mid ci p1 p2))
  47.           (setq pp2 (mid ci p2 p3))
  48.           (setq pp3 (mid ci p3 p4))
  49.           (setq pp4 (mid ci p4 p1))
  50.           (cond
  51.             ( (vl-some '(lambda ( li ) (equal (unit (mapcar '- p p1)) (unit (mapcar '- p (cadr li))) 1e-6)) lil)
  52.               (if (not (equal p pp3 1e-6))
  53.                 (setq mp pp3)
  54.                 (setq mp p3)
  55.               )
  56.             )
  57.             ( (vl-some '(lambda ( li ) (equal (unit (mapcar '- p p2)) (unit (mapcar '- p (cadr li))) 1e-6)) lil)
  58.               (if (not (equal p pp4 1e-6))
  59.                 (setq mp pp4)
  60.                 (setq mp p4)
  61.               )
  62.             )
  63.             ( (vl-some '(lambda ( li ) (equal (unit (mapcar '- p p3)) (unit (mapcar '- p (cadr li))) 1e-6)) lil)
  64.               (if (not (equal p pp1 1e-6))
  65.                 (setq mp pp1)
  66.                 (setq mp p1)
  67.               )
  68.             )
  69.             ( (vl-some '(lambda ( li ) (equal (unit (mapcar '- p p4)) (unit (mapcar '- p (cadr li))) 1e-6)) lil)
  70.               (if (not (equal p pp2 1e-6))
  71.                 (setq mp pp2)
  72.                 (setq mp p2)
  73.               )
  74.             )
  75.             ( (vl-some '(lambda ( li ) (equal (unit (mapcar '- p pp1)) (unit (mapcar '- p (cadr li))) 1e-6)) lil)
  76.               (if (not (equal p pp 1e-6))
  77.                 (setq mp pp)
  78.                 (setq mp pp3)
  79.               )
  80.             )
  81.             ( (vl-some '(lambda ( li ) (equal (unit (mapcar '- p pp2)) (unit (mapcar '- p (cadr li))) 1e-6)) lil)
  82.               (if (not (equal p pp 1e-6))
  83.                 (setq mp pp)
  84.                 (setq mp pp4)
  85.               )
  86.             )
  87.             ( (vl-some '(lambda ( li ) (equal (unit (mapcar '- p pp3)) (unit (mapcar '- p (cadr li))) 1e-6)) lil)
  88.               (if (not (equal p pp 1e-6))
  89.                 (setq mp pp)
  90.                 (setq mp pp1)
  91.               )
  92.             )
  93.             ( (vl-some '(lambda ( li ) (equal (unit (mapcar '- p pp4)) (unit (mapcar '- p (cadr li))) 1e-6)) lil)
  94.               (if (not (equal p pp 1e-6))
  95.                 (setq mp pp)
  96.                 (setq mp pp2)
  97.               )
  98.             )
  99.           )
  100.         )
  101.         ( (= (length pl) 3)
  102.           (setq p1 (car pl))
  103.           (setq p2 (cadr pl))
  104.           (setq p3 (caddr pl))
  105.           (mapcar 'set '(p1 p2 p3) (vl-sort (list p1 p2 p3) '(lambda ( a b ) (< (vlax-curve-getparamatpoint ci a) (vlax-curve-getparamatpoint ci b)))))
  106.           (setq pp1 (mid ci p1 p2))
  107.           (setq pp2 (mid ci p2 p3))
  108.           (setq pp3 (mid ci p3 p1))
  109.           (cond
  110.             ( (vl-some '(lambda ( li ) (equal (unit (mapcar '- p p1)) (unit (mapcar '- p (cadr li))) 1e-6)) lil)
  111.               (if (not (equal p pp2 1e-6))
  112.                 (setq mp pp2)
  113.                 (setq mp p2)
  114.               )
  115.             )
  116.             ( (vl-some '(lambda ( li ) (equal (unit (mapcar '- p p2)) (unit (mapcar '- p (cadr li))) 1e-6)) lil)
  117.               (if (not (equal p pp3 1e-6))
  118.                 (setq mp pp3)
  119.                 (setq mp p3)
  120.               )
  121.             )
  122.             ( (vl-some '(lambda ( li ) (equal (unit (mapcar '- p p3)) (unit (mapcar '- p (cadr li))) 1e-6)) lil)
  123.               (if (not (equal p pp1 1e-6))
  124.                 (setq mp pp1)
  125.                 (setq mp p1)
  126.               )
  127.             )
  128.             ( (vl-some '(lambda ( li ) (equal (unit (mapcar '- p pp1)) (unit (mapcar '- p (cadr li))) 1e-6)) lil)
  129.               (if (not (equal p pp 1e-6))
  130.                 (setq mp pp)
  131.                 (setq mp p3)
  132.               )
  133.             )
  134.             ( (vl-some '(lambda ( li ) (equal (unit (mapcar '- p pp2)) (unit (mapcar '- p (cadr li))) 1e-6)) lil)
  135.               (if (not (equal p pp 1e-6))
  136.                 (setq mp pp)
  137.                 (setq mp p1)
  138.               )
  139.             )
  140.             ( (vl-some '(lambda ( li ) (equal (unit (mapcar '- p pp3)) (unit (mapcar '- p (cadr li))) 1e-6)) lil)
  141.               (if (not (equal p pp 1e-6))
  142.                 (setq mp pp)
  143.                 (setq mp p2)
  144.               )
  145.             )
  146.           )
  147.         )
  148.         ( (= (length pl) 2)
  149.           (setq p1 (car pl))
  150.           (setq p2 (cadr pl))
  151.           (mapcar 'set '(p1 p2) (vl-sort (list p1 p2) '(lambda ( a b ) (< (vlax-curve-getparamatpoint ci a) (vlax-curve-getparamatpoint ci b)))))
  152.           (setq mp (mid ci p1 p2))
  153.         )
  154.         ( (= (length pl) 1)
  155.           (setq mp (car pl))
  156.         )
  157.       )
  158.     )
  159.     (if (and ci (not (vlax-erased-p ci)))
  160.       (entdel ci)
  161.     )
  162.     (list p mp)
  163.   )
  164.  
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on October 06, 2023, 07:52:43 AM
This is as far as I get with (mc) sub... Still the complete routine has many lacks, but in essence it's attempt to shorten coding as much as possible and give desired result as much fine as it could with custom (_offset) sub function... In attachment is *.lsp, but you have it also at cadtutor site download section...

Code - Auto/Visual Lisp: [Select]
  1.   (defun mc ( p lw lww lwww / mid ci pl pll1 pll2 plll pllll mp p1 p2 p3 p4 p121 p231 p341 p141 p131 p241 p122 p232 p342 p142 p132 p242 pp1 pp2 pp3 pp4 ppp1 ppp2 ppp3 ppp4 )
  2.  
  3.     (defun mid ( ci p1 p2 / par1 par2 mp )
  4.       (setq par1 (+ (* 2 pi) (vlax-curve-getparamatpoint ci p1)) par2 (+ (* 2 pi) (vlax-curve-getparamatpoint ci p2)))
  5.       (setq mp (mapcar '+ '(0.0 0.0) (vlax-curve-getpointatparam ci (/ (+ par1 par2) 2.0))))
  6.     )
  7.  
  8.     (if (and (setq ci (entmakex (list (cons 0 "CIRCLE") (cons 10 p) (cons 40 0.5)))) lw (not (vlax-erased-p ci)) (not (vlax-erased-p lw)))
  9.       (setq pl (vlax-invoke (vlax-ename->vla-object ci) 'intersectwith (vlax-ename->vla-object lw) acextendnone))
  10.     )
  11.     (setq pll1 (vlax-invoke (vlax-ename->vla-object lww) 'intersectwith (vlax-ename->vla-object lww) acextendnone))
  12.     (setq pll1 (groupbynum pll1 3))
  13.     (setq pll1 (mapcar '(lambda ( x ) (list (car x) (cadr x))) pll1))
  14.     (setq pll2 (vlax-invoke (vlax-ename->vla-object lwww) 'intersectwith (vlax-ename->vla-object lwww) acextendnone))
  15.     (setq pll2 (groupbynum pll2 3))
  16.     (setq pll2 (mapcar '(lambda ( x ) (list (car x) (cadr x))) pll2))
  17.     (setq plll (vlax-invoke (vlax-ename->vla-object ci) 'intersectwith (vlax-ename->vla-object lww) acextendnone))
  18.     (setq plll (groupbynum plll 3))
  19.     (setq plll (mapcar '(lambda ( x ) (list (car x) (cadr x))) plll))
  20.     (setq pllll (vlax-invoke (vlax-ename->vla-object ci) 'intersectwith (vlax-ename->vla-object lwww) acextendnone))
  21.     (setq pllll (groupbynum pllll 3))
  22.     (setq pllll (mapcar '(lambda ( x ) (list (car x) (cadr x))) pllll))
  23.     (if pl
  24.       (cond
  25.         ( (= (length pl) 12)
  26.           (setq p1 (list (car pl) (cadr pl)))
  27.           (setq p2 (list (nth 3 pl) (nth 4 pl)))
  28.           (setq p3 (list (nth 6 pl) (nth 7 pl)))
  29.           (setq p4 (list (nth 9 pl) (nth 10 pl)))
  30.           (mapcar 'set '(p1 p2 p3 p4) (vl-sort (list p1 p2 p3 p4) '(lambda ( a b ) (< (vlax-curve-getparamatpoint ci a) (vlax-curve-getparamatpoint ci b)))))
  31.           (if (equal (vlax-curve-getparamatpoint ci p4) (* 2 pi) 1e-6)
  32.             (mapcar 'set '(p1 p2 p3 p4) (list p4 p1 p2 p3))
  33.           )
  34.           (setq p121 (mid ci p1 p2))
  35.           (setq p231 (mid ci p2 p3))
  36.           (setq p341 (mid ci p3 p4))
  37.           (setq p141 (mid ci p1 p4))
  38.           (setq p131 (mid ci p1 p3))
  39.           (setq p241 (mid ci p2 p4))
  40.           (setq p122 (polar p (angle p121 p) 0.5))
  41.           (setq p232 (polar p (angle p231 p) 0.5))
  42.           (setq p342 (polar p (angle p341 p) 0.5))
  43.           (setq p142 (polar p (angle p141 p) 0.5))
  44.           (setq p132 (polar p (angle p131 p) 0.5))
  45.           (setq p242 (polar p (angle p241 p) 0.5))
  46.           (setq pp1 (vl-remove-if-not '(lambda ( x ) (equal x p1 0.1)) plll))
  47.           (setq pp2 (vl-remove-if-not '(lambda ( x ) (equal x p2 0.1)) plll))
  48.           (setq pp3 (vl-remove-if-not '(lambda ( x ) (equal x p3 0.1)) plll))
  49.           (setq pp4 (vl-remove-if-not '(lambda ( x ) (equal x p4 0.1)) plll))
  50.           (setq ppp1 (vl-remove-if-not '(lambda ( x ) (equal x p1 0.1)) pllll))
  51.           (setq ppp2 (vl-remove-if-not '(lambda ( x ) (equal x p2 0.1)) pllll))
  52.           (setq ppp3 (vl-remove-if-not '(lambda ( x ) (equal x p3 0.1)) pllll))
  53.           (setq ppp4 (vl-remove-if-not '(lambda ( x ) (equal x p4 0.1)) pllll))
  54.           (entdel lw)
  55.           (entdel lwww)
  56.           (entdel ci)
  57.           (setvar 'aperture 25)
  58.           (cond
  59.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p121)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p121)) pll2)) (not (collinear-pp p121 p (cadar lil))) (not (collinear-pp p121 p (cadadr lil))) (not (equal (distance p1 p2) 1.0 1e-6)))
  60.               (setq mp p121)
  61.             )
  62.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p122)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p122)) pll2)) (not (collinear-pp p122 p (cadar lil))) (not (collinear-pp p122 p (cadadr lil))) (not (equal (distance p1 p2) 1.0 1e-6)))
  63.               (setq mp p122)
  64.             )
  65.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p231)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p231)) pll2)) (not (collinear-pp p231 p (cadar lil))) (not (collinear-pp p231 p (cadadr lil))) (not (equal (distance p2 p3) 1.0 1e-6)))
  66.               (setq mp p231)
  67.             )
  68.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p232)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p232)) pll2)) (not (collinear-pp p232 p (cadar lil))) (not (collinear-pp p232 p (cadadr lil))) (not (equal (distance p2 p3) 1.0 1e-6)))
  69.               (setq mp p232)
  70.             )
  71.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p341)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p341)) pll2)) (not (collinear-pp p341 p (cadar lil))) (not (collinear-pp p341 p (cadadr lil))) (not (equal (distance p3 p4) 1.0 1e-6)))
  72.               (setq mp p341)
  73.             )
  74.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p342)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p342)) pll2)) (not (collinear-pp p342 p (cadar lil))) (not (collinear-pp p342 p (cadadr lil))) (not (equal (distance p3 p4) 1.0 1e-6)))
  75.               (setq mp p342)
  76.             )
  77.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p141)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p141)) pll2)) (not (collinear-pp p141 p (cadar lil))) (not (collinear-pp p141 p (cadadr lil))) (not (equal (distance p1 p4) 1.0 1e-6)))
  78.               (setq mp p141)
  79.             )
  80.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p142)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p142)) pll2)) (not (collinear-pp p142 p (cadar lil))) (not (collinear-pp p142 p (cadadr lil))) (not (equal (distance p1 p4) 1.0 1e-6)))
  81.               (setq mp p142)
  82.             )
  83.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p131)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p131)) pll2)) (not (collinear-pp p131 p (cadar lil))) (not (collinear-pp p131 p (cadadr lil))) (not (equal (distance p1 p3) 1.0 1e-6)))
  84.               (setq mp p131)
  85.             )
  86.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p132)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p132)) pll2)) (not (collinear-pp p132 p (cadar lil))) (not (collinear-pp p132 p (cadadr lil))) (not (equal (distance p1 p3) 1.0 1e-6)))
  87.               (setq mp p132)
  88.             )
  89.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p241)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p241)) pll2)) (not (collinear-pp p241 p (cadar lil))) (not (collinear-pp p241 p (cadadr lil))) (not (equal (distance p2 p4) 1.0 1e-6)))
  90.               (setq mp p241)
  91.             )
  92.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p242)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p242)) pll2)) (not (collinear-pp p242 p (cadar lil))) (not (collinear-pp p242 p (cadadr lil))) (not (equal (distance p2 p4) 1.0 1e-6)))
  93.               (setq mp p242)
  94.             )
  95.             ( (and (= (length pp1) 2) (not (collinear-pp p1 p (cadar lil))) (not (collinear-pp p1 p (cadadr lil))))
  96.               (setq mp p1)
  97.             )
  98.             ( (and (= (length pp2) 2) (not (collinear-pp p2 p (cadar lil))) (not (collinear-pp p2 p (cadadr lil))))
  99.               (setq mp p2)
  100.             )
  101.             ( (and (= (length pp3) 2) (not (collinear-pp p3 p (cadar lil))) (not (collinear-pp p3 p (cadadr lil))))
  102.               (setq mp p3)
  103.             )
  104.             ( (and (= (length pp4) 2) (not (collinear-pp p4 p (cadar lil))) (not (collinear-pp p4 p (cadadr lil))))
  105.               (setq mp p4)
  106.             )
  107.             ( (and (= (length pp1) 1) (= (length ppp1) 1) (not (collinear-pp p1 p (cadar lil))) (not (collinear-pp p1 p (cadadr lil))))
  108.               (setq mp p1)
  109.             )
  110.             ( (and (= (length pp2) 1) (= (length ppp2) 1) (not (collinear-pp p2 p (cadar lil))) (not (collinear-pp p2 p (cadadr lil))))
  111.               (setq mp p2)
  112.             )
  113.             ( (and (= (length pp3) 1) (= (length ppp3) 1) (not (collinear-pp p3 p (cadar lil))) (not (collinear-pp p3 p (cadadr lil))))
  114.               (setq mp p3)
  115.             )
  116.             ( (and (= (length pp4) 1) (= (length ppp4) 1) (not (collinear-pp p4 p (cadar lil))) (not (collinear-pp p4 p (cadadr lil))))
  117.               (setq mp p4)
  118.             )
  119.             ( (setq mp (vl-some '(lambda ( x ) (if (equal (osnap p "_int") (list (car x) (cadr x) 0.0) 1e-6) x)) (vl-remove-if '(lambda ( x ) (vl-some '(lambda ( y ) (equal x y 1e-6)) (mapcar 'cdr (vl-remove-if '(lambda ( z ) (/= (car z) 10)) (entget lww))))) pll1)))
  120.             )
  121.           )
  122.           (setvar 'aperture ape)
  123.         )
  124.         ( (= (length pl) 9)
  125.           (setq p1 (list (car pl) (cadr pl)))
  126.           (setq p2 (list (nth 3 pl) (nth 4 pl)))
  127.           (setq p3 (list (nth 6 pl) (nth 7 pl)))
  128.           (mapcar 'set '(p1 p2 p3) (vl-sort (list p1 p2 p3) '(lambda ( a b ) (< (vlax-curve-getparamatpoint ci a) (vlax-curve-getparamatpoint ci b)))))
  129.           (if (equal (vlax-curve-getparamatpoint ci p3) (* 2 pi) 1e-6)
  130.             (mapcar 'set '(p1 p2 p3) (list p3 p1 p2))
  131.           )
  132.           (setq p121 (mid ci p1 p2))
  133.           (setq p231 (mid ci p2 p3))
  134.           (setq p131 (mid ci p1 p3))
  135.           (setq p122 (polar p (angle p121 p) 0.5))
  136.           (setq p232 (polar p (angle p231 p) 0.5))
  137.           (setq p132 (polar p (angle p131 p) 0.5))
  138.           (setq pp1 (vl-remove-if-not '(lambda ( x ) (equal x p1 0.1)) plll))
  139.           (setq pp2 (vl-remove-if-not '(lambda ( x ) (equal x p2 0.1)) plll))
  140.           (setq pp3 (vl-remove-if-not '(lambda ( x ) (equal x p3 0.1)) plll))
  141.           (setq ppp1 (vl-remove-if-not '(lambda ( x ) (equal x p1 0.1)) pllll))
  142.           (setq ppp2 (vl-remove-if-not '(lambda ( x ) (equal x p2 0.1)) pllll))
  143.           (setq ppp3 (vl-remove-if-not '(lambda ( x ) (equal x p3 0.1)) pllll))
  144.           (entdel lw)
  145.           (entdel lwww)
  146.           (entdel ci)
  147.           (setvar 'aperture 25)
  148.           (cond
  149.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p121)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p121)) pll2)) (not (collinear-pp p121 p (cadar lil))) (not (collinear-pp p121 p (cadadr lil))) (not (equal (distance p1 p2) 1.0 1e-6)))
  150.               (setq mp p121)
  151.             )
  152.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p122)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p122)) pll2)) (not (collinear-pp p122 p (cadar lil))) (not (collinear-pp p122 p (cadadr lil))) (not (equal (distance p1 p2) 1.0 1e-6)))
  153.               (setq mp p122)
  154.             )
  155.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p131)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p131)) pll2)) (not (collinear-pp p131 p (cadar lil))) (not (collinear-pp p131 p (cadadr lil))) (not (equal (distance p1 p3) 1.0 1e-6)))
  156.               (setq mp p131)
  157.             )
  158.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p132)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p132)) pll2)) (not (collinear-pp p132 p (cadar lil))) (not (collinear-pp p132 p (cadadr lil))) (not (equal (distance p1 p3) 1.0 1e-6)))
  159.               (setq mp p132)
  160.             )
  161.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p231)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p231)) pll2)) (not (collinear-pp p231 p (cadar lil))) (not (collinear-pp p231 p (cadadr lil))) (not (equal (distance p2 p3) 1.0 1e-6)))
  162.               (setq mp p231)
  163.             )
  164.             ( (and (or (vl-some '(lambda ( x ) (collinear-p p x p232)) pll1) (vl-some '(lambda ( x ) (collinear-p p x p232)) pll2)) (not (collinear-pp p232 p (cadar lil))) (not (collinear-pp p232 p (cadadr lil))) (not (equal (distance p2 p3) 1.0 1e-6)))
  165.               (setq mp p232)
  166.             )
  167.             ( (and (= (length pp1) 2) (not (collinear-pp p1 p (cadar lil))) (not (collinear-pp p1 p (cadadr lil))))
  168.               (setq mp p1)
  169.             )
  170.             ( (and (= (length pp2) 2) (not (collinear-pp p2 p (cadar lil))) (not (collinear-pp p2 p (cadadr lil))))
  171.               (setq mp p2)
  172.             )
  173.             ( (and (= (length pp3) 2) (not (collinear-pp p3 p (cadar lil))) (not (collinear-pp p3 p (cadadr lil))))
  174.               (setq mp p3)
  175.             )
  176.             ( (and (= (length pp1) 1) (= (length ppp1) 1) (not (collinear-pp p1 p (cadar lil))) (not (collinear-pp p1 p (cadadr lil))))
  177.               (setq mp p1)
  178.             )
  179.             ( (and (= (length pp2) 1) (= (length ppp2) 1) (not (collinear-pp p2 p (cadar lil))) (not (collinear-pp p2 p (cadadr lil))))
  180.               (setq mp p2)
  181.             )
  182.             ( (and (= (length pp3) 1) (= (length ppp3) 1) (not (collinear-pp p3 p (cadar lil))) (not (collinear-pp p3 p (cadadr lil))))
  183.               (setq mp p3)
  184.             )
  185.             ( (setq mp (vl-some '(lambda ( x ) (if (equal (osnap p "_int") (list (car x) (cadr x) 0.0) 1e-6) x)) (vl-remove-if '(lambda ( x ) (vl-some '(lambda ( y ) (equal x y 1e-6)) (mapcar 'cdr (vl-remove-if '(lambda ( z ) (/= (car z) 10)) (entget lww))))) pll1)))
  186.             )
  187.           )
  188.           (setvar 'aperture ape)
  189.         )
  190.         ( (= (length pl) 6)
  191.           (setq p1 (list (car pl) (cadr pl)))
  192.           (setq p2 (list (nth 3 pl) (nth 4 pl)))
  193.           (setq p12 (mid ci p1 p2))
  194.           (if (and (equal p1 p2 1e-6) (not (collinear-pp p1 p (cadar lil))) (not (collinear-pp p1 p (cadadr lil))))
  195.             (setq mp p1)
  196.           )
  197.           (if (not mp)
  198.             (setq mp p12)
  199.           )
  200.         )
  201.         ( (= (length pl) 3)
  202.           (setq mp (list (car pl) (cadr pl)))
  203.         )
  204.       )
  205.     )
  206.     (if (and ci (not (vlax-erased-p ci)))
  207.       (entdel ci)
  208.     )
  209.     (list p mp)
  210.   )
  211.  

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on October 11, 2023, 04:00:29 AM
Can someone analyze my roof.lsp attached in previous post further more... In some cases works fine, but in some not - some lines are missing, or calculated point is wrong... Still I am satisfied and like it is, but strongly believe that it could be brought to almost excellent - this way it's mark is good...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on October 13, 2023, 09:41:12 AM
I don't have feedbacks...
@Ron, have you tested latest one called just : roof.lsp...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on October 13, 2023, 03:16:59 PM
This one is short and it's based on EXTRUDE - TAPER option... Very much like Gian Paolo Cattaneo's, but it's shorter and I think just a little more reliable...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:roof-simple ( / *error* ucsf cmd ape osm ang ch delob el ell k lin linn p p1 p2 pl pll enx pp ss sss ssn vs ti )
  2.  
  3.   (defun *error* ( m )
  4.     (if delob
  5.       (setvar 'delobj delob)
  6.     )
  7.     (if osm
  8.       (setvar 'osmode osm)
  9.     )
  10.     (if ape
  11.       (setvar 'aperture ape)
  12.     )
  13.     (if ucsf
  14.       (if command-s
  15.         (command-s "_.ucs" "_p")
  16.         (vl-cmdf "_.ucs" "_p")
  17.       )
  18.     )
  19.     (if (= 8 (logand 8 (getvar 'undoctl)))
  20.       (if command-s
  21.         (command-s "_.undo" "_e")
  22.         (vl-cmdf "_.undo" "_e")
  23.       )
  24.     )
  25.     (if cmd
  26.       (setvar 'cmdecho cmd)
  27.     )
  28.     (if m
  29.       (prompt m)
  30.     )
  31.     (princ)
  32.   )
  33.  
  34.   (setq cmd (getvar 'cmdecho))
  35.   (setvar 'cmdecho 0)
  36.   (setq delob (getvar 'delobj))
  37.   (setq osm (getvar 'osmode))
  38.   (setvar 'osmode 0)
  39.   (setq ape (getvar 'aperture))
  40.   (setvar 'aperture 15)
  41.   (if (= 0 (getvar 'worlducs))
  42.     (progn
  43.       (vl-cmdf "_.ucs" "_w")
  44.       (setq ucsf t)
  45.     )
  46.   )
  47.   (if (= 8 (logand 8 (getvar 'undoctl)))
  48.     (vl-cmdf "_.undo" "_e")
  49.   )
  50.   (vl-cmdf "_.undo" "_m")
  51.   (setq pll (entsel "\nPick closed polygonal LWPOLYLINE with straight segments..."))
  52.   (if (and pll (= (cdr (assoc 0 (setq enx (entget (car pll))))) "LWPOLYLINE") (= 1 (logand 1 (cdr (assoc 70 enx)))) (vl-every '(lambda ( x ) (= (cdr x) 0.0)) (vl-remove-if '(lambda ( x ) (/= (car x) 42)) enx)))
  53.     (progn
  54.       (setq pl (car pll))
  55.       (setq pp (cadr pll))
  56.       (setq p (getpoint "\nPick point inside picked 2d polyline"))
  57.       (setq p (list (car p) (cadr p) 1e-3))
  58.       (initget "2D 3D")
  59.       (setq ch (cond ( (getkword "\nEnter choice [2D / 3D] < 2D > : ") ) ("2D")))
  60.       (setq ti (car (_vl-times)))
  61.       (if (= ch "2D")
  62.         (progn
  63.           (setvar 'delobj 0)
  64.           (vl-cmdf "_.zoom" "_v")
  65.           (setq vs (getvar 'viewsize))
  66.           (vl-cmdf "_.zoom" "_p")
  67.           (setq el (entlast))
  68.           (vl-cmdf "_.extrude" pl "" "_t" 45.0 0.1)
  69.           (while (< 0 (getvar 'cmdactive))
  70.             (vl-cmdf "")
  71.           )
  72.           (vl-cmdf "_.solidedit" "_f" "_m" p "" (list 0.0 0.0 0.0) (list 0.0 0.0 vs))
  73.           (while (< 0 (getvar 'cmdactive))
  74.             (vl-cmdf "")
  75.           )
  76.           (vl-cmdf "_.solidedit" "_b" "_p" (entlast))
  77.           (while (< 0 (getvar 'cmdactive))
  78.             (vl-cmdf "")
  79.           )
  80.           (while (setq el (entnext el))
  81.             (if (and (= (cdr (assoc 0 (entget el))) "3DSOLID") (not (eq el (ssname (ssget (osnap pp "_nea") '((0 . "3DSOLID"))) 0))))
  82.               (entdel el)
  83.               (setq ell el)
  84.             )
  85.           )
  86.           (if (= (cdr (assoc 0 (entget ell))) "3DSOLID")
  87.             (progn
  88.               (setvar 'delobj 1)
  89.               (setq el (entlast))
  90.               (vl-cmdf "_.xedges" "_l")
  91.               (while (< 0 (getvar 'cmdactive))
  92.                 (vl-cmdf "")
  93.               )
  94.               (entdel ell)
  95.               (setq sss (ssadd) ss (ssadd))
  96.               (while (setq el (entnext el))
  97.                 (ssadd el ss)
  98.               )
  99.               (repeat (setq ssn (sslength ss))
  100.                 (setq lin (ssname ss (setq ssn (1- ssn))))
  101.                 (if (and (= (caddr (cdr (assoc 10 (entget lin)))) 0.0) (= (caddr (cdr (assoc 11 (entget lin)))) 0.0))
  102.                   (entdel lin)
  103.                   (progn
  104.                     (setq p1 (cdr (assoc 10 (entget lin))))
  105.                     (setq p2 (cdr (assoc 11 (entget lin))))
  106.                     (setq p1 (list (car p1) (cadr p1) 0.0))
  107.                     (setq p2 (list (car p2) (cadr p2) 0.0))
  108.                     (entmod (subst (cons 10 p1) (assoc 10 (entget lin)) (entget lin)))
  109.                     (setq linn (entupd (cdr (assoc -1 (entmod (subst (cons 11 p2) (assoc 11 (entget lin)) (entget lin)))))))
  110.                     (ssadd linn sss)
  111.                   )
  112.                 )
  113.               )
  114.               (repeat (setq ssn (sslength sss))
  115.                 (setq lin (ssname sss (setq ssn (1- ssn))))
  116.                 (repeat (setq k ssn)
  117.                   (setq linn (ssname sss (setq k (1- k))))
  118.                   (if (or (and (equal (cdr (assoc 10 (entget lin))) (cdr (assoc 10 (entget linn))) 1e-6) (equal (cdr (assoc 11 (entget lin))) (cdr (assoc 11 (entget linn))) 1e-6)) (and (equal (cdr (assoc 10 (entget lin))) (cdr (assoc 11 (entget linn))) 1e-6) (equal (cdr (assoc 11 (entget lin))) (cdr (assoc 10 (entget linn))) 1e-6)))
  119.                     (entdel lin)
  120.                   )
  121.                 )
  122.               )
  123.             )
  124.           )
  125.         )
  126.         (progn
  127.           (setvar 'delobj 0)
  128.           (initget 5)
  129.           (setq ang (getreal "\nEnter angle of slope of roof in decimal degrees (0 < ang < 90) : "))
  130.           (setq ang (rem (- 90.0 ang) 90.0))
  131.           (vl-cmdf "_.regen")
  132.           (vl-cmdf "_.zoom" "_v")
  133.           (setq vs (getvar 'viewsize))
  134.           (vl-cmdf "_.zoom" "_p")
  135.           (setq el (entlast))
  136.           (vl-cmdf "_.extrude" pl "" "_t" ang 0.1)
  137.           (while (< 0 (getvar 'cmdactive))
  138.             (vl-cmdf "")
  139.           )
  140.           (vl-cmdf "_.solidedit" "_f" "_m" p "" (list 0.0 0.0 0.0) (list 0.0 0.0 vs))
  141.           (while (< 0 (getvar 'cmdactive))
  142.             (vl-cmdf "")
  143.           )
  144.           (vl-cmdf "_.solidedit" "_b" "_p" (entlast))
  145.           (while (< 0 (getvar 'cmdactive))
  146.             (vl-cmdf "")
  147.           )
  148.           (while (setq el (entnext el))
  149.             (if (and (= (cdr (assoc 0 (entget el))) "3DSOLID") (not (eq el (ssname (ssget (osnap pp "_nea") '((0 . "3DSOLID"))) 0))))
  150.               (entdel el)
  151.             )
  152.           )
  153.         )
  154.       )
  155.       (prompt "\nElapsed time : ") (princ (rtos (- (car (_vl-times)) ti) 2 15)) (prompt " milliseconds...")
  156.     )
  157.   )
  158.   (*error* nil)
  159. )
  160.  

Regards, M.R.
HTH.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on December 08, 2023, 03:00:29 PM
@Lee, @Highflyingbird

I have a question for both : Were you using Taper option of EXTRUDE command (built-in) variant, or pure calculation method...
I am very interested to see if some of you are going to shed some light on my attempts which are good, but pretty slow... So this all could be very basic ground for developing *.arx, or *.dll... And if you don't mind I'd like to see speed comparison, so we need to have all codes public... For me ad my opinion, no one would like to pay anything if routines are slow and not practical which is the case with my versions posted at www.cadtutor.net download section...
Expecting new feedbacks... Topic again overcrowded with new topics...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: BIGAL on December 08, 2023, 07:04:23 PM
I am impressed with your roof function years ago did a roof for houses programs you enter roof pitch and it uses pface to make 3d Pface roof panels so get the impression of a solid roof. It did only 1 section at a time HIP, GABLE, VALLEY and so on just building each section. Will make an example. It used geometry to work out the roof ridge points in 3D. It may be a simpler way than using extruded solids and merging. Will make some shapes use your roof.lsp to make 2d Line work then try to convert to 3d.

The code is copyrighted. So can not post.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on December 18, 2023, 11:43:53 PM
@It's Alive, aka Daniel,
Can those 3 files be converted from *.lsp to *.arx and not change it's functionality only fasten speed of execution...
I'll be very grateful if that's possible and AFAIK I think that every code is possible to be converted... Am I wrong or right?
Nevertheless, both are copyrighted on me as author, so I can public them as I wish and want whenever I find appropriate...
I am specifically interested in conversion of : roof2d-new-new-new.lsp and roof2d-new-new-solutions.lsp...

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on December 28, 2023, 09:36:17 AM
@Daniel,
Am I right or wrong?
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: kdub_nz on December 28, 2023, 07:30:12 PM
@ribarm,
Hi,
The @prefix does not notify the named member here as it does on the AutoCAD forums.
We  are relying on the member visiting the post thread and noticing the posted message.

It may be best to send Daniel a PM with a link to  your post.

//
Generally it is possible to write cpp code to replicate lisp, but the exercise is not a straight forward translation and takes a good understanding of both languages.
Lisp these days is pretty fast , however
some gains may be made by replacing any known bottlrnecks with a call to an wrapped arx method. This will require a thorough analysis of the code blocks that are slow or repeatedly called in loops or for multiple entities.
My belief is that a real return on investment is questionable for code that has minimal usage.
. . . this is exacerbated when working with compiled languages that are version dependant.

Sometimes we spend a lot of time trying to save a couple milliseconds.

This attachment is from one of my favourite sites.
Though comical in nature, it puts the issue in perspective:


Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on December 29, 2023, 05:13:00 AM
The problem is that I haven't saved any time by shortening the code - roof2d-new-new-new.lsp...
I just used what already was in roof2d-new-new.lsp...
Also the problem is that I have some places where I am using double (foreach) one nested in parent one. This slows things very much and I don't know how to get rid of this snippet... 2droof-final.lsp which can be founded in *.ZIP I posted in download section of www.cadtutor.net works perfectly fast, you just have to unlock it by finding winning combination of loop; errn; errm inputs...

Here I am attaching my testing *.DWG that I used in that purposes - there are simple tasks and there are complex ultimate roof tasks...
So who is willing to do testings on some fast PC, he/she can add new examples...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on December 30, 2023, 06:24:55 AM
@kdub_nz

Thank you for reply... The reason I am asking for conversion to some other language is not saving of couple milliseconds, but couple of 15 minutes, or even more half hours, hours... You can see what I am speaking if you download my previously posted *.dwg and those *.lsp files posted prevoiously in my latest post with attachments with *.lsp files...
Anyway, thanks for noticed working plan graph, although for me not totally understandable... The only person that I know that should be capeable to do conversion are Daniel for *.arx, *.brx and Gilles C. for *.dll and Sean T. also for *.dll... But I am not doing anyones roll-call, just making this topic alive as much as it should be...

Regards and thanks for reading,
M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: kdub_nz on December 30, 2023, 01:47:33 PM
I'll Translate the graph,

To get a return in 5 years on your time/money spent :

If the routine is used once a day and you can save 5 minutes each usage,
you can spend 6 days working on it.

If the routine is used once a week and you can save 1 minutes each usage,
you can spend 4 hours working on it.

If the routine is used once a month and you can save 5 minutes each usage,
you can spend 5 hours working on it.

some of the results are rounded with poetic licence.

If there are 10 paying users, all the better :)
As I said, perspective.

added:
Of course, the lessons learnt from programming are invaluable, so usually it's worth the time :)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: BIGAL on December 30, 2023, 05:55:47 PM
I downloaded the roof2d-new-new-solutions.lsp it has "(defun Vl-sort" twice I think second needs to be maybe Vl-sorti ?

Not sure what 1st Vl-sort is doing, I have a sort done by others that allows any number of sort elements used for sorting lists. Very fast.

I have no idea if (nth x lst) is faster than (cadr lst) etc but every millisecond adds up.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on December 31, 2023, 09:05:30 AM
@BIGAL,

Please refer to this topic for (vl-sort) issue : https://www.theswamp.org/index.php?topic=58952.0
When coded for (vl-sort), vl-sort that is coded has higher priority than built-in (vl-sort)... I only left my version previously named (_vl-sort) in case that someone that is coding for vl-sort in Object Arx, or Dot Net can actually find it useful...

P.S. Since you downloaded only *-solutions.lsp, you firstly have to have single solution to make *-solutions.lsp avoid that case... It's not you have to download all, but roof2d-new-new-new.lsp should give you that single solution, or 2droof-final.lsp, or roof2d-new-new.lsp and from practical reasons I strongly suggest that you download complete package for HIPPED ROOFS at www.cadtutor.net download section : https://www.cadtutor.net/forum/files/file/36-hipped-roof-routines/
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 04, 2024, 01:40:40 PM
Here is the version I recently started over again... It matches the speed I need, but unfortunately if throws giberish of lines and the way I coded it is supposed to do on non-ortho hipped roofs... The main problem lies from line 470 to the end... So if someone is willing to help he/she is welcomed...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:roof ( / *error* func wcs initvalueslst ucsf unit rlw unioncollinearplaneprints unique car-sort inside-p distp2t vl-sort numbpos ti s dm lw lwi pl pli tl utl lst ll lll x y ip ips ipss pos ang d1 d2 d t1 t2 tt k done flag f lil )
  2.  
  3.   (defun *error* ( m )
  4.     (foreach li (unique lil)
  5.       (if (and (car li) (cadr li) (not (equal (car li) (cadr li) 1e-6)))
  6.         (entmake (list (cons 0 "LINE") (cons 10 (car li)) (cons 11 (cadr li))))
  7.       )
  8.     )
  9.     (if (and lwi (not (vlax-erased-p lwi)))
  10.       (if (= (type lwi) (quote ename))
  11.         (entdel lwi)
  12.         (vla-delete lwi)
  13.       )
  14.     )
  15.     (if wcs
  16.       (if ucsf
  17.         (while
  18.           (not
  19.             (and
  20.               (equal (getvar (quote ucsxdir)) (car ucsf) 1e-6)
  21.               (equal (getvar (quote ucsydir)) (cadr ucsf) 1e-6)
  22.               (equal (trans (list 0.0 0.0 1.0) 1 0 t) (caddr ucsf) 1e-6)
  23.             )
  24.           )
  25.           (exe (list "_.UCS" "_P"))
  26.         )
  27.       )
  28.     )
  29.     (while (= 8 (logand 8 (getvar (quote undoctl))))
  30.       (if (not (exe (list "_.UNDO" "_E")))
  31.         (if doc
  32.           (vla-endundomark doc)
  33.         )
  34.       )
  35.     )
  36.     (if initvalueslst
  37.       (mapcar (function apply_cadr->car) initvalueslst)
  38.     )
  39.     (foreach fun (list (quote func) (quote vl-load) (quote exe) (quote cmdfun) (quote cmderr) (quote catch_cont) (quote apply_cadr->car) (quote ftoa))
  40.       (setq fun nil)
  41.     )
  42.     (if doc
  43.       (vla-regen doc acactiveviewport)
  44.     )
  45.     (if m
  46.       (prompt m)
  47.     )
  48.     (princ)
  49.   )
  50.  
  51.   (defun func ( wcs / sysvarpreset sysvarlst sysvarvals ) ;;; wcs (T/nil) ;;; cad, doc, alo, spc - global variables (Visual Lisp main VLA-OBJECT pointers) ;;; vl-load exe cmdfun cmderr catch_cont apply_cadr->car ftoa - library sub functions common for standard template initialization ;;;
  52.  
  53.     (defun vl-load nil
  54.       (or cad
  55.           (setq cad (vlax-get-acad-object))
  56.           (progn
  57.             (vl-load-com)
  58.             (setq cad (vlax-get-acad-object))
  59.           )
  60.         )
  61.       )
  62.       (or doc (setq doc (vla-get-activedocument cad)))
  63.       (or alo (setq alo (vla-get-activelayout doc)))
  64.       (or spc (setq spc (vla-get-block alo)))
  65.     )
  66.  
  67.     ;;; sometimes not needed to use/initialize AxiveX Visual Lisp extensions - (comment/uncomment) following line ;;;
  68.     (or (and cad doc alo spc) (vl-load))
  69.  
  70.     (defun exe ( tokenslist )
  71.       ( (lambda ( tokenslist / ctch )
  72.           (if (vl-catch-all-error-p (setq ctch (cmdfun tokenslist t)))
  73.             (progn
  74.               (cmderr tokenslist)
  75.               (catch_cont ctch)
  76.             )
  77.             (progn
  78.               (while (< 0 (getvar (quote cmdactive)))
  79.                 (vl-cmdf "")
  80.               )
  81.               t
  82.             )
  83.           )
  84.         )
  85.         tokenslist
  86.       )
  87.     )
  88.  
  89.     (defun cmdfun ( tokenslist flag / ctch ) ;;; tokenslist - command parameters list of strings ;;; flag - if "t" specified, upon successful execution returns t, otherwise if "nil" specified, return is always nil no matter what outcome of function execution is - it should be successful anyway if specified tokenslist was hardcoded correctly... ;;;
  90.       (if command-s
  91.         (if flag
  92.           (if (not (vl-catch-all-error-p (setq ctch (vl-catch-all-apply (function command-s) tokenslist))))
  93.             flag
  94.             ctch
  95.           )
  96.           (if (vl-catch-all-error-p (setq ctch (vl-catch-all-apply (function command-s) tokenslist)))
  97.             ctch
  98.           )
  99.         )
  100.         (if flag
  101.           (if (not (vl-catch-all-error-p (setq ctch (vl-catch-all-apply (function vl-cmdf) tokenslist))))
  102.             flag
  103.             ctch
  104.           )
  105.           (if (vl-catch-all-error-p (setq ctch (vl-catch-all-apply (function command) tokenslist)))
  106.             ctch
  107.           )
  108.         )
  109.       )
  110.     )
  111.  
  112.     (defun cmderr ( tokenslist ) ;;; tokenslist - list of tokens representing command syntax at which used (cmdfun) failed with successful execution ;;;
  113.       (prompt (strcat "\ncommand execution failure... error at used command tokenslist : " (vl-prin1-to-string tokenslist)))
  114.     )
  115.  
  116.     (defun catch_cont ( ctch / gr )
  117.       (prompt "\nleft mouse click to continue or enter to generate catch error - ESC to break...")
  118.       (while
  119.         (and
  120.           (vl-catch-all-error-p (or ctch (setq ctch (vl-catch-all-apply (function /) (list 1 0)))))
  121.           (setq gr (grread))
  122.           (/= (car gr) 3)
  123.           (not (equal gr (list 2 13)))
  124.         )
  125.       )
  126.       (if (vl-catch-all-error-p ctch)
  127.         ctch
  128.       )
  129.     )
  130.  
  131.     (defun apply_cadr->car ( sysvarvaluepair / ctch )
  132.       (setq ctch (vl-catch-all-apply (function setvar) sysvarvaluepair))
  133.       (if (vl-catch-all-error-p ctch)
  134.         (progn
  135.           (prompt (strcat "\ncatched error on setting system variable : " (vl-prin1-to-string (vl-symbol-name (car sysvarvaluepair))) " with value : " (vl-prin1-to-string (cadr sysvarvaluepair))))
  136.           (catch_cont ctch)
  137.         )
  138.       )
  139.     )
  140.  
  141.     (defun ftoa ( n / m a s b )
  142.       (if (numberp n)
  143.         (progn
  144.           (setq m (fix ((if (< n 0) - +) n 1e-8)))
  145.           (setq a (abs (- n m)))
  146.           (setq m (itoa m))
  147.           (setq s "")
  148.           (while (and (not (equal a 0.0 1e-6)) (setq b (fix (* a 10.0))))
  149.             (setq s (strcat s (itoa b)))
  150.             (setq a (- (* a 10.0) b))
  151.           )
  152.           (if (= (type n) (quote int))
  153.             m
  154.             (if (= s "")
  155.               m
  156.               (if (and (= m "0") (< n 0))
  157.                 (strcat "-" m "." s)
  158.                 (strcat m "." s)
  159.               )
  160.             )
  161.           )
  162.         )
  163.       )
  164.     )
  165.  
  166.     (setq sysvarpreset
  167.       (list
  168.         (list (quote cmdecho) 0)
  169.         (list (quote 3dosmode) 0)
  170.         (list (quote osmode) 0)
  171.         (list (quote unitmode) 0)
  172.         (list (quote cmddia) 0)
  173.         (list (quote ucsvp) 0)
  174.         (list (quote ucsortho) 0)
  175.         (list (quote projmode) 0)
  176.         (list (quote orbitautotarget) 0)
  177.         (list (quote insunits) 0)
  178.         (list (quote hpseparate) 0)
  179.         (list (quote hpgaptol) 0)
  180.         (list (quote halogap) 0)
  181.         (list (quote edgemode) 0)
  182.         (list (quote pickdrag) 0)
  183.         (list (quote qtextmode) 0)
  184.         (list (quote dragsnap) 0)
  185.         (list (quote angdir) 0)
  186.         (list (quote aunits) 0)
  187.         (list (quote limcheck) 0)
  188.         (list (quote gridmode) 0)
  189.         (list (quote nomutt) 0)
  190.         (list (quote apbox) 0)
  191.         (list (quote attdia) 0)
  192.         (list (quote blipmode) 0)
  193.         (list (quote copymode) 0)
  194.         (list (quote circlerad) 0.0)
  195.         (list (quote filletrad) 0.0)
  196.         (list (quote filedia) 1)
  197.         (list (quote autosnap) 1)
  198.         (list (quote objectisolationmode) 1)
  199.         (list (quote highlight) 1)
  200.         (list (quote lispinit) 1)
  201.         (list (quote layerpmode) 1)
  202.         (list (quote fillmode) 1)
  203.         (list (quote dragmodeinterrupt) 1)
  204.         (list (quote dispsilh) 1)
  205.         (list (quote fielddisplay) 1)
  206.         (list (quote deletetool) 1)
  207.         (list (quote delobj) 1)
  208.         (list (quote dblclkedit) 1)
  209.         (list (quote attreq) 1)
  210.         (list (quote explmode) 1)
  211.         (list (quote frameselection) 1)
  212.         (list (quote ltgapselection) 1)
  213.         (list (quote pickfirst) 1)
  214.         (list (quote plinegen) 1)
  215.         (list (quote plinetype) 1)
  216.         (list (quote peditaccept) 1)
  217.         (list (quote solidcheck) 1)
  218.         (list (quote visretain) 1)
  219.         (list (quote regenmode) 1)
  220.         (list (quote celtscale) 1.0)
  221.         (list (quote ltscale) 1.0)
  222.         (list (quote osnapcoord) 2)
  223.         (list (quote grips) 2)
  224.         (list (quote dragmode) 2)
  225.         (list (quote lunits) 2)
  226.         (list (quote pickstyle) 3)
  227.         (list (quote navvcubedisplay) 3)
  228.         (list (quote pickauto) 3)
  229.         (list (quote draworderctl) 3)
  230.         (list (quote expert) 5)
  231.         (list (quote auprec) 6)
  232.         (list (quote luprec) 6)
  233.         (list (quote pickbox) 6)
  234.         (list (quote aperture) 6)
  235.         (list (quote osoptions) 7)
  236.         (list (quote dimzin) 8)
  237.         (list (quote pdmode) 35)
  238.         (list (quote pdsize) -1.5)
  239.         (list (quote celweight) -1)
  240.         (list (quote cecolor) "BYLAYER")
  241.         (list (quote celtype) "ByLayer")
  242.         (list (quote clayer) "0")
  243.       )
  244.     )
  245.     (setq sysvarlst (mapcar (function car) sysvarpreset))
  246.     (setq sysvarvals (mapcar (function cadr) sysvarpreset))
  247.     (setq sysvarvals
  248.       (vl-remove nil
  249.         (mapcar
  250.           (function (lambda ( x )
  251.             (if (getvar x) (nth (vl-position x sysvarlst) sysvarvals))
  252.           ))
  253.           sysvarlst
  254.         )
  255.       )
  256.     )
  257.     (setq sysvarlst
  258.       (vl-remove-if-not
  259.         (function (lambda ( x )
  260.           (getvar x)
  261.         ))
  262.         sysvarlst
  263.       )
  264.     )
  265.     (setq initvalueslst
  266.       (apply (function mapcar)
  267.         (cons (function list)
  268.           (list
  269.             sysvarlst
  270.             (mapcar (function getvar) sysvarlst)
  271.           )
  272.         )
  273.       )
  274.     )
  275.       (cons (function setvar)
  276.         (list
  277.           sysvarlst
  278.           sysvarvals
  279.         )
  280.       )
  281.     )
  282.     (while (= 8 (logand 8 (getvar (quote undoctl))))
  283.       (if (not (exe (list "_.UNDO" "_E")))
  284.         (if doc
  285.           (vla-endundomark doc)
  286.         )
  287.       )
  288.     )
  289.     (if (not (exe (list "_.UNDO" "_M")))
  290.       (if doc
  291.         (vla-startundomark doc)
  292.       )
  293.     )
  294.     (if wcs
  295.       (if (= 0 (getvar (quote worlducs)))
  296.         (progn
  297.           (setq ucsf
  298.             (list
  299.               (getvar (quote ucsxdir))
  300.               (getvar (quote ucsydir))
  301.               (trans (list 0.0 0.0 1.0) 1 0 t)
  302.             )
  303.           )
  304.           (exe (list "_.UCS" "_W"))
  305.         )
  306.       )
  307.     )
  308.     wcs
  309.   )
  310.  
  311.   (defun unit ( v / d )
  312.     (if (not (equal (setq d (distance '(0.0 0.0 0.0) v)) 0.0 1e-6))
  313.       (mapcar '(lambda ( x ) (/ x d)) v)
  314.     )
  315.   )
  316.  
  317.   (defun rlw ( lw / e x1 x2 x3 x4 x5 x6 )
  318.     ;; by ElpanovEvgeniy
  319.     (if (= (cdr (assoc 0 (setq e (entget lw)))) "LWPOLYLINE")
  320.       (progn
  321.         (foreach a1 e
  322.           (cond
  323.             ( (= (car a1) 10) (setq x2 (cons a1 x2)) )
  324.             ( (= (car a1) 40) (setq x4 (cons (cons 41 (cdr a1)) x4)) )
  325.             ( (= (car a1) 41) (setq x3 (cons (cons 40 (cdr a1)) x3)) )
  326.             ( (= (car a1) 42) (setq x5 (cons (cons 42 (- (cdr a1))) x5)) )
  327.             ( (= (car a1) 210) (setq x6 (cons a1 x6)) )
  328.             ( t (setq x1 (cons a1 x1)) )
  329.           )
  330.         )
  331.         (entmod
  332.           (append (reverse x1)
  333.             (append
  334.               (apply 'append
  335.                 (apply 'mapcar
  336.                   (cons 'list
  337.                     (list x2
  338.                       (cdr (reverse (cons (car x3) (reverse x3))))
  339.                       (cdr (reverse (cons (car x4) (reverse x4))))
  340.                       (cdr (reverse (cons (car x5) (reverse x5))))
  341.                     )
  342.                   )
  343.                 )
  344.               )
  345.               x6
  346.             )
  347.           )
  348.         )
  349.         (entupd lw)
  350.       )
  351.     )
  352.   )
  353.  
  354.   (defun unioncollinearplaneprints ( tl / a b tll )
  355.     (while (setq a (car tl))
  356.       (setq b (vl-remove-if-not (function (lambda ( x ) (and (collinear-pp (car a) (cadr a) (car x)) (collinear-pp (car a) (cadr a) (cadr x))))) tl))
  357.       (setq tll (cons a tll))
  358.       (if b
  359.         (setq tl (vl-remove-if (function (lambda ( x ) (vl-some (function (lambda ( y ) (equal x y 1e-6))) b))) tl))
  360.         (setq tl (cdr tl))
  361.       )
  362.     )
  363.     tll
  364.   )
  365.  
  366.   (defun unique ( lst / a ll )
  367.     (while (setq a (car lst))
  368.       (if (vl-some (function (lambda ( x ) (equal x a 1e-6))) (cdr lst))
  369.         (setq ll (cons a ll) lst (vl-remove-if (function (lambda ( x ) (equal x a 1e-6))) (cdr lst)))
  370.         (setq ll (cons a ll) lst (cdr lst))
  371.       )
  372.     )
  373.     (reverse ll)
  374.   )
  375.  
  376.   (defun car-sort ( lst cmp / rtn )
  377.     (setq rtn (car lst))
  378.     (foreach itm (cdr lst)
  379.       (if (apply cmp (list itm rtn))
  380.         (setq rtn itm)
  381.       )
  382.     )
  383.     rtn
  384.   )
  385.  
  386.   (defun inside-p ( p lw lwi )
  387.   )
  388.  
  389.   (defun distp2t ( p gg / i d )
  390.     (if (setq i (inters p (polar p (+ (angle (car gg) (cadr gg)) (* 0.5 pi)) 1.0) (car gg) (cadr gg) nil))
  391.       (setq d (distance p i))
  392.     )
  393.   )
  394.  
  395.   (defun vl-sort ( lst func )
  396.     (mapcar
  397.       (function (lambda ( x ) (nth x lst)))
  398.       (vl-sort-i lst func)
  399.     )
  400.   )
  401.  
  402.   (defun numbpos ( n len )
  403.     (cond
  404.       ( (< n len)
  405.         n
  406.       )
  407.       ( (> n len)
  408.         (- n len)
  409.       )
  410.       ( t 0 )
  411.     )
  412.   )
  413.  
  414.   (setq wcs (func t)) ;;; starting "library" template sub function - initialization ;;;
  415.   (prompt "\nPick closed polygonal LWPOLYLINE on unlocked layer...")
  416.   (if (setq s (ssget "_+.:E:S:L" (list (cons 0 "LWPOLYLINE") (cons -4 "&=") (cons 70 1) (cons -4 "<not") (cons -4 "<>") (cons 42 0.0) (cons -4 "not>"))))
  417.     (progn
  418.       (setq ti (car (_vl-times)))
  419.       (setq dm 1e+308)
  420.       (setq lw (ssname s 0))
  421.       (vlax-invoke (vlax-ename->vla-object lw) (quote offset) -1e-3)
  422.       (setq lwi (entlast))
  423.       (if (> (vlax-curve-getarea lwi) (vlax-curve-getarea lw))
  424.         (progn
  425.           (rlw lw)
  426.           (entdel lwi)
  427.           (vlax-invoke (vlax-ename->vla-object lw) (quote offset) -1e-3)
  428.           (setq lwi (entlast))
  429.         )
  430.       )
  431.       (setq pl (mapcar (function cdr) (vl-remove-if (function (lambda ( x ) (/= (car x) 10))) (entget lw))))
  432.       (setq pli (mapcar (function cdr) (vl-remove-if (function (lambda ( x ) (/= (car x) 10))) (entget lwi))))
  433.       (while (not (equal (car pl) (car pli) 0.1))
  434.         (setq pli (append (cdr pli) (list (car pli))))
  435.       )
  436.       (setq tl (mapcar (function (lambda ( a b ) (list a b))) pl (append (cdr pl) (list (car pl)))))
  437.       ;(setq utl (unioncollinearplaneprints tl))
  438.       (setq lst (mapcar (function (lambda ( a b c d ) (list a (angle a b) c d))) pl pli (cons (last tl) (reverse (cdr (reverse tl)))) tl))
  439.       (while (not done)
  440.         (if (not flag)
  441.           (progn
  442.             (setq ll lst ips nil dm 1e+308)
  443.             (while (setq x (car ll))
  444.               (setq ll (cdr ll))
  445.               (setq y (car ll))
  446.               (if (and x y (setq ip (inters (car x) (polar (car x) (cadr x) 1.0) (car y) (polar (car y) (cadr y) 1.0) nil)) (inside-p ip lw lwi))
  447.                 (if (< (setq d (min (distp2t ip (caddr x)) (distp2t ip (last y)))) dm)
  448.                   (setq ips (list ip x y) dm d)
  449.                 )
  450.               )
  451.             )
  452.             (setq pos (vl-position (cadr ips) lst))
  453.             (if pos
  454.               (repeat (numbpos pos (length pl))
  455.                 (setq lst (append (cdr lst) (list (car lst))))
  456.               )
  457.             )
  458.             (setq tt (vl-remove-if (function (lambda ( x ) (= (length (vl-remove-if (function (lambda ( y ) (equal x y 1e-6))) (list (caddr (cadr ips)) (cadddr (cadr ips)) (caddr (caddr ips)) (cadddr (caddr ips))))) 2))) (list (caddr (cadr ips)) (cadddr (cadr ips)) (caddr (caddr ips)) (cadddr (caddr ips)))))
  459.             ;(setq tl (vl-remove (car (vl-remove-if (function (lambda ( x ) (vl-some (function (lambda ( y ) (equal x y 1e-6))) tt))) (list (caddr (cadr ips)) (cadddr (cadr ips)) (caddr (caddr ips)) (cadddr (caddr ips))))) tl))
  460.             (if (and (car ips) (car (cadr ips)) (car (caddr ips)))
  461.               (setq lil (cons (list (car ips) (car (cadr ips))) lil) lil (cons (list (car ips) (car (caddr ips))) lil))
  462.             )
  463.             (setq ang (angle (car ips) (inters (caar tt) (cadar tt) (caadr tt) (cadadr tt) nil)))
  464.             (setq lst (cons (setq ips (list (car ips) ang (car tt) (cadr tt))) lst))
  465.           )
  466.         )
  467.         (setq flag t)
  468.         (setq ll lst dm 1e+308)
  469.         (setq x (assoc (caar lil) ll))
  470.         (setq ll (vl-remove x ll))
  471.         (while (setq y (car ll))
  472.           (setq ll (cdr ll))
  473.           (if (and x y (setq ip (inters (car x) (polar (car x) (cadr x) 1.0) (car y) (polar (car y) (cadr y) 1.0) nil)))
  474.             (if (and (not (equal (setq d (distance ip (caar lil))) 0.0 1e-6)) (< d dm) (inside-p ip lw lwi))
  475.               (setq ips (list ip x y) dm d)
  476.             )
  477.           )
  478.         )
  479.         (setq pos (vl-position (list (cadadr lil) (cadar lil)) tl))
  480.         (setq tl (vl-remove (list (cadadr lil) (cadar lil)) tl))
  481.         (if (and pos (not f))
  482.           (repeat (numbpos pos (length pl))
  483.             (setq lst (append (cdr lst) (list (car lst))))
  484.             (setq tl (append (cdr tl) (list (car tl))))
  485.           )
  486.         )
  487.         (setq f t)
  488.         (setq tt (vl-some (function (lambda ( x ) (if (member x (last tl)) (last tl)))) (mapcar (function cadr) lil)))
  489.         (setq tl (vl-remove tt tl))
  490.         (setq t1 (car tl))
  491.         (setq t2 (last tl))
  492.         (if tl
  493.           (progn
  494.             (setq ang (angle (car ips) (inters (car t1) (cadr t1) (car t2) (cadr t2) nil)))
  495.             (setq ips (list (car ips) ang t1 t2))
  496.             (setq lst (cons ips lst))
  497.             (setq lil (cons (list (car ips) (caar lil)) lil))
  498.             (foreach l lst
  499.               (if (equal (angle (car l) (car ips)) (cadr l) 1e-6)
  500.                 (setq lil (cons (list (car ips) (car l)) lil))
  501.               )
  502.             )
  503.           )
  504.         )
  505.         (if (not k)
  506.           (setq k 1)
  507.           (setq k (1+ k))
  508.         )
  509.         (if (equal k (length pl))
  510.           (setq done t)
  511.         )
  512.       )
  513.       (prompt "\nElapsed time : ") (prompt (ftoa (- (car (_vl-times)) ti))) (prompt " milliseconds...")
  514.       (prompt "\nFor UNDO - type \"UNDO\" - \"Back\" option...")
  515.     )
  516.   )
  517.   (*error* nil)
  518. )
  519.  

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 06, 2024, 01:43:17 PM
So, here in attachment is revised version of code I published... But it doesn't finish or just start to some point... Speed is satisfactory, but I would like to see some *.arx, *.brx, *.dll on the same theme...

Regards and stay well in New 2024 Year...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: kdub_nz on January 06, 2024, 03:55:40 PM
@ribarm,

What is your definition of
Quote
non-ortho hipped roofs...

Best new year wishes.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 06, 2024, 07:09:56 PM
@ribarm,

What is your definition of
Quote
non-ortho hipped roofs...

Best new year wishes.

The random acute or obtuse angles - without 90 degrees between 2 segments of polygonal closed LWPOLYLINE...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: kdub_nz on January 06, 2024, 10:04:18 PM
If I recall correctly from my bin and chute (plate steelwork ) days, for equal angle faces the hip/valley line will always bisect the corner angle,
and logic says that a ridge line will always be central between parallel walls , so I can't imagine a roof will be any different [ except turned upside down ]

But programming for my work discipline is different to yours.

Your definition is as I imagined, but I thought I'd just check terminology :)
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 09, 2024, 04:24:23 AM
Well, no one want to participate... Then how do you expect that need for improvement will happen...
I posted 3 files here : https://www.theswamp.org/index.php?topic=41837.msg618211#msg618211
And no one didn't even download not to mention looked it...
I explained to some point where I need help... It is not that I don't want to write it to the finish... The problem is that variable that holds planes points are removed sooner than should and removal of planes from planes list is the way it should function...
Someone with great expirience should really take look at those 3 attempts (lisps)...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: kdub_nz on January 09, 2024, 02:23:11 PM
Marco,
I didn't know you required assistance, I thought your posts were progress reports (ignoring the 'Challenge' in the title :) ).

Could you re-post one file that works best and the one that you are having issues with ( and describe the issues. )

. . . at least pointing people in the right direction may encourage assistance.

I assume we can select any outline amongst the several posted.

Because I haven't read through the thread;
Does the outline represent the eaves line ?
Where is the roof pitch being set ? (if at all . . I noticed a mention of a plane, so ? )
Do you have any procedural requirements ?

Regards,
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 09, 2024, 03:15:08 PM
The last one I coded is "roof-2chk-n.lsp"... But it has lacks that I can't figure why exist... It doesn't complete the job... My thougts were pointed in direction of writing faster version then currently the one that finishes job in slow time period... It is called "roof2d-new-new-new.lsp", but I realized that all we, I needed was faster LISP processing which is acomplished with Bricsys BricsCAD... Anyway there is realy fast one, but it fails in search for finding right combination of variables when starting search... It is called "2droof-final.lsp" and it also completes the job correctly, even better than "roof2d-new-new-new.lsp"... All this you can find in *.ZIP I posted at www.cadtutor.net - download section... So, if someone has spare time, I'd suggest trying to fix "roof-2chk-n.lsp"... There could be perhaps progress in gaining better time execution...

Thanks for interes and Happy Holidays...
M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 12, 2024, 01:37:59 PM
I am realy stacked here with "roof-2chk-n.lsp"... Can someone check it from line 653 - roof-chk-n.lsp / 642 - roof-chk-nn.lsp to the end of file... In my opinion it should work, but it isn't - it just starts and then stops after 6 lines drawn... If someone has spare time, please download it from this post : https://www.theswamp.org/index.php?topic=41837.msg618211#msg618211

Your help is always welcomed...
M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 13, 2024, 01:35:10 PM
As you can see, I've added my latest "roof-2chk-nn.lsp"... So, expecting any feedback... Still it's buggy and sometimes though it starts correctly, may make unexpected mistake(s) - so I've added OVERKILL, just for overlaps, though it may create line(s) that are without any relation to roof solution...

Latest version attached to this post...
 :-( :mrgreen:
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: pkohut on January 14, 2024, 04:55:10 AM
I am realy stacked here with "roof-2chk-n.lsp"... Can someone check it from line 653 - roof-chk-n.lsp / 642 - roof-chk-nn.lsp to the end of file... In my opinion it should work, but it isn't - it just starts and then stops after 6 lines drawn... If someone has spare time, please download it from this post : https://www.theswamp.org/index.php?topic=41837.msg618211#msg618211

Your help is always welcomed...
M.R.

Hey Marco,

I added some logging to the code base and collected output, both are in the attached zip file.   This should help.


Code: [Select]
;;; printf debugging added by Paul Kohut, 1/14/2024
;;; Purpose: track down why code is not outputting the correct
;;; roof ridge lines. The debug output sent to the acad console,
;;; tags will help tie to the source code/variables/entities.
;;;
;;; The cpatured data was weakly tested against some key subrointes,
;;; all of them preformed as expected.
;;; Original code provided by Marko Ribar in HIPPED ROOF ROUTINES.ZIP

I think this is the problem spot.
Code: [Select]
(setq lst (mapcar (function (lambda ( a b c d ) (list a (angle a b) c d))) pl pli (cons (last tl) (reverse (cdr (reverse tl)))) tl))
        (princ (strcat "\natt=" (itoa att) "  lst length=" (itoa (length lst))))
        (print lst)
;;; pk - I think at this point the first 7 entity are rerun a
;;; few times creating duplicate ridge geometry in lst.
;;; These long complex lines should get reformed so debugging
;;; in VLISP or VS Code is easier.

Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 14, 2024, 11:59:24 AM
@pkohut

I saw attachment, but reposting the same file I attach doesn't solve things... I posted files in hope that they could be used purpously in task for completion of whole ridge solution on any non-ortho closed polygonal LWPOLYLINE... Nevertheless I attached few more at *.ZIP at cadtutor... So now I wait cos' I don't know why it stops after drawing 6 lines... At the end I'll give up with those roof-*.lsp... So still I am hoping that "roof2d-new-new-new.lsp" could be converted to *.arx, *.brx, or *.dll...

Have a nice day...
M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: pkohut on January 14, 2024, 02:52:13 PM
@pkohut

I saw attachment, but reposting the same file I attach doesn't solve things... I posted files in hope that they could be used purpously in task for completion of whole ridge solution on any non-ortho closed polygonal LWPOLYLINE... Nevertheless I attached few more at *.ZIP at cadtutor... So now I wait cos' I don't know why it stops after drawing 6 lines... At the end I'll give up with those roof-*.lsp... So still I am hoping that "roof2d-new-new-new.lsp" could be converted to *.arx, *.brx, or *.dll...

Have a nice day...
M.R.

So, I spent about 10 hours looking at the code trying to see how it works.  Additions were made, to your file, to facilitate collecting data for analysis.  That "changed" lisp file is included in my zip attachment. This is just one of the steps I might go through when looking at an unknown problem space.   So, the lisp file I attached is not the same as your original.

Given the additional data points, my intent was to look at why the lst variable is not populated properly.  Either I'll get lucky figuring it out on my own or maybe someone will chime in with support.  Once the program is creating the correct results I can move on to optimizations.

Speaking of optimizations, one of my collected data points was for the entmakex of circle and entdel of said circle, which is called about 750 times. Calling entmake(x) is not very fast and one of the vl- create routines would be much faster.  Since the circles are just used for their built in geometry routines, only 2 circles are needed.  Create them early, then modify their radius and center points  everywhere entmakex is called.

Converting "roof2d-new-new-new.lsp" to ARX can be done by someone familiar with ARX. Someone would have to go routine by routine and do mental gymnastics from Autolisp to ARX.  I am not aware of any automated task to do such a conversion.  The benefit of such a conversion, faster speed, will be lost because the nested loops run deep recalculating same values over and over.   I was going to spend a few cycles doing the conversion to ARX for a straight on head to head comparison.  After that I was going to investigate different collection algorithms for huge speed improvements.

I don't think I have anything else useful to contribute.

Later.
That's all I got to say
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 14, 2024, 06:49:37 PM
@pkohut

Thanks for interes into this topic... I think I've slightly improved speed of "roof2d-new-new-new.lsp"... The newest version is called "roof2d-new-new-new-n.lsp" and it's posted in ZIP I uploaded at cadtutor download section... Nothing spectacular - removing testing subs for checking for correct finish, added 2 subs - (offd) with (chkcircinside-dist) and adding shorter chk for correct finish in single line :
(vl-every (function (lambda ( x ) (vl-some (function (lambda ( y ) (equal x y 1e-6))) (unique (mapcar (function cadr) lil))))) pl)

That's all from me for now...
Like BIGAL - every millisecond counts...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: kdub_nz on January 14, 2024, 07:34:08 PM
@ribarm

Quote
posted in ZIP I uploaded at cadtutor download section

I'm old and cranky, so . .

why do you assume that everyone is a member there, and would travel to pick it up ??

I'm noticing this attitude more and more.

This is a peer to peer forum where responsibility to roles is expected to be honoured, and an example set for newer members.

Regards,
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: pkohut on January 14, 2024, 10:17:01 PM
That's all from me for now...
Like BIGAL - every millisecond counts...
M.R.

Sadly, I think, 12 years chasing those milliseconds has given you tunnel vision against better solutions.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 15, 2024, 02:27:14 AM
@ribarm

Quote
posted in ZIP I uploaded at cadtutor download section

I'm old and cranky, so . .

why do you assume that everyone is a member there, and would travel to pick it up ??

I'm noticing this attitude more and more.

This is a peer to peer forum where responsibility to roles is expected to be honoured, and an example set for newer members.

Regards,

I don't understand...
Shell I post *.ZIP here?

OK. You can find it here : https://www.theswamp.org/index.php?topic=41837.msg619616#msg619616 in attachment...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 22, 2024, 11:01:01 AM
I've succedded only few lines more on tst.dwg which is packed in *.ZIP... The problem is (processlil) sub function of "roof-2chk-nnnnnnn.lsp" that has (cond) which is doing things from start until hit first that satisfies condition... But in my case we don't know is firstly one be correct choice or some else that also satisfies condition but not placed above the one it hits firstly... What is also different is that I used now "lstt" variable for storing solutions and "lst" is points of LWPOLYLIINE data with angles and adjacent plane prints... So since I made possible on that specific case to acomplish little better results, I am satisfied... As no one is not interested to improve and fix what's done, I may say that I am finished here...
It was my pleasure to lead conversation on this challenge - we all learned something from it...
Thanks for reading and stay well and happy...
M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on January 24, 2024, 03:53:40 PM
I abandoned (cond) in (processlil) sub and made it more concise and acceptable to what I tried to achieve... But something still isn't good - in my tst.dwg it draws only few lines and doesn't complete solution... I am afraid that I can't swim out of mug that is draining me... For now I am abandning coding, but am posting in code tags, so that concise version can be seen and maybe someone can chime in and reply with constructive idea(s)...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:roof-2chk-nnnnnnnnnn-s ( / *error* vl-load rlw unioncollinearplaneprints unique inside-p distp2t _vl-sort numbpos offd chkcircinside-dist chkcircinside collinear-pp searchipss processlil process ti s dm lw lwi pl ppl pli tl utl lst llst lstt ll zz x y ip ips ipss pos ang angp flag d t1 t2 tt1 tt2 tt k n att pll lil lilx lil1 lil2 )
  2.  
  3.   (defun *error* ( m )
  4.     ;(command-s "_.-OVERKILL" "_ALL" "" "_T" "_Y" "")
  5.     (if (and lwi (not (vlax-erased-p lwi)))
  6.       (if (= (type lwi) (quote ename))
  7.         (entdel lwi)
  8.         (vla-delete lwi)
  9.       )
  10.     )
  11.     (while (= 8 (logand 8 (getvar (quote undoctl))))
  12.       (if (not (vl-cmdf "_.UNDO" "_E"))
  13.         (if doc
  14.           (vla-endundomark doc)
  15.         )
  16.       )
  17.     )
  18.     (prompt "\nElapsed time : ") (prompt (rtos (- (car (_vl-times)) ti) 2 20)) (prompt " milliseconds...")
  19.     (prompt "\nFor UNDO - type \"UNDO\" - \"Back\" option...")
  20.     (if m
  21.       (prompt m)
  22.     )
  23.     (princ)
  24.   )
  25.  
  26.   (defun vl-load nil
  27.     (or cad
  28.         (setq cad (vlax-get-acad-object))
  29.         (progn
  30.           (vl-load-com)
  31.           (setq cad (vlax-get-acad-object))
  32.         )
  33.       )
  34.     )
  35.     (or doc (setq doc (vla-get-activedocument cad)))
  36.     (or alo (setq alo (vla-get-activelayout doc)))
  37.     (or spc (setq spc (vla-get-block alo)))
  38.   )
  39.  
  40.   (defun rlw ( lw / e x1 x2 x3 x4 x5 x6 )
  41.     ;; by ElpanovEvgeniy
  42.     (if (= (cdr (assoc 0 (setq e (entget lw)))) "LWPOLYLINE")
  43.       (progn
  44.         (foreach a1 e
  45.           (cond
  46.             ( (= (car a1) 10) (setq x2 (cons a1 x2)) )
  47.             ( (= (car a1) 40) (setq x4 (cons (cons 41 (cdr a1)) x4)) )
  48.             ( (= (car a1) 41) (setq x3 (cons (cons 40 (cdr a1)) x3)) )
  49.             ( (= (car a1) 42) (setq x5 (cons (cons 42 (- (cdr a1))) x5)) )
  50.             ( (= (car a1) 210) (setq x6 (cons a1 x6)) )
  51.             ( t (setq x1 (cons a1 x1)) )
  52.           )
  53.         )
  54.         (entmod
  55.           (append (reverse x1)
  56.             (append
  57.               (apply (function append)
  58.                 (apply (function mapcar)
  59.                   (cons (function list)
  60.                     (list x2
  61.                       (cdr (reverse (cons (car x3) (reverse x3))))
  62.                       (cdr (reverse (cons (car x4) (reverse x4))))
  63.                       (cdr (reverse (cons (car x5) (reverse x5))))
  64.                     )
  65.                   )
  66.                 )
  67.               )
  68.               x6
  69.             )
  70.           )
  71.         )
  72.         (entupd lw)
  73.       )
  74.     )
  75.   )
  76.  
  77.   (defun unioncollinearplaneprints ( tl / a b tll )
  78.     (while (setq a (car tl))
  79.       (setq b (vl-remove-if-not (function (lambda ( x ) (and (collinear-pp (car a) (cadr a) (car x)) (collinear-pp (car a) (cadr a) (cadr x))))) tl))
  80.       (setq tll (cons a tll))
  81.       (if b
  82.         (setq tl (vl-remove-if (function (lambda ( x ) (vl-some (function (lambda ( y ) (equal x y 1e-6))) b))) tl))
  83.         (setq tl (cdr tl))
  84.       )
  85.     )
  86.     tll
  87.   )
  88.  
  89.   (defun unique ( lst / a ll )
  90.     (while (setq a (car lst))
  91.       (if (vl-some (function (lambda ( x ) (equal x a 1e-2))) (cdr lst))
  92.         (setq ll (cons a ll) lst (vl-remove-if (function (lambda ( x ) (equal x a 1e-2))) (cdr lst)))
  93.         (setq ll (cons a ll) lst (cdr lst))
  94.       )
  95.     )
  96.     (reverse ll)
  97.   )
  98.  
  99.   (defun inside-p ( p lw lwi )
  100.   )
  101.  
  102.   (defun distp2t ( p gg / i d )
  103.     (if (setq i (inters p (polar p (+ (angle (car gg) (cadr gg)) (* 0.5 pi)) 1.0) (car gg) (cadr gg) nil))
  104.       (setq d (distance p i))
  105.     )
  106.   )
  107.  
  108.   (defun _vl-sort ( lst func )
  109.     (mapcar
  110.       (function (lambda ( x ) (nth x lst)))
  111.       (vl-sort-i lst func)
  112.     )
  113.   )
  114.  
  115.   (defun numbpos ( n len )
  116.     (cond
  117.       ( (< n len)
  118.         n
  119.       )
  120.       ( (> n len)
  121.         (- n len)
  122.       )
  123.       ( t 0 )
  124.     )
  125.   )
  126.  
  127.   (defun offd ( ip tl / ff d dl )
  128.  
  129.     (defun ff ( ip d )
  130.       (if (not (chkcircinside-dist ip d))
  131.         (progn
  132.           (setq dl (vl-remove-if (function (lambda ( x ) (equal d x 1e-6))) dl))
  133.           (ff ip
  134.             (setq d
  135.               (vl-some
  136.                 (function
  137.                   (lambda ( x )
  138.                     (if
  139.                       (>
  140.                         (-
  141.                           (length dl)
  142.                           (length (vl-remove-if (function (lambda ( y ) (equal x y 1e-5))) dl))
  143.                         ) 2
  144.                       )
  145.                       x
  146.                     )
  147.                   )
  148.                 )
  149.                 (_vl-sort dl (function >))
  150.               )
  151.             )
  152.           )
  153.         )
  154.         d
  155.       )
  156.     )
  157.  
  158.     (setq dl
  159.       (mapcar
  160.         (function
  161.           (lambda ( x )
  162.             (distance ip
  163.               (inters
  164.                 ip
  165.                 (polar ip (+ (* 0.5 pi) (angle (car x) (cadr x))) 1.0)
  166.                 (car x)
  167.                 (polar (car x) (angle (car x) (cadr x)) 1.0)
  168.                 nil
  169.               )
  170.             )
  171.           )
  172.         )
  173.         tl
  174.       )
  175.     )
  176.     (setq d
  177.       (vl-some
  178.         (function
  179.           (lambda ( x )
  180.             (if
  181.               (>
  182.                 (-
  183.                   (length dl)
  184.                   (length (vl-remove-if (function (lambda ( y ) (equal x y 1e-5))) dl))
  185.                 ) 2
  186.               )
  187.               x
  188.             )
  189.           )
  190.         )
  191.         (_vl-sort dl (function >))
  192.       )
  193.     )
  194.     (ff ip d)
  195.   )
  196.  
  197.   (defun chkcircinside-dist ( pp dist / ci ipp ippl params pts tst )
  198.     (if (and pp dist)
  199.       (progn
  200.         (setq ci (entmakex (list (cons 0 "CIRCLE") (cons 10 pp) (cons 40 dist))))
  201.         (setq params (list 0.0 (/ pi 6.0) (/ pi 3.0) (* 0.5 pi) (* 2.0 (/ pi 3.0)) (* 5.0 (/ pi 6.0)) pi (* 7.0 (/ pi 6.0)) (* 4.0 (/ pi 3.0)) (* 1.5 pi) (* 5.0 (/ pi 3.0)) (* 11.0 (/ pi 6.0))))
  202.         (if (setq ipp (vlax-invoke (vlax-ename->vla-object ci) (quote intersectwith) (vlax-ename->vla-object lw) acextendnone))
  203.           (progn
  204.             (while ipp
  205.               (setq ippl (cons (list (car ipp) (cadr ipp)) ippl))
  206.               (setq ipp (cdddr ipp))
  207.             )
  208.             (if ippl
  209.               (setq params (append params (mapcar (function (lambda ( x ) (vlax-curve-getparamatpoint ci x))) ippl)))
  210.             )
  211.             (setq pts (apply (function append) (mapcar (function (lambda ( x ) (list (vlax-curve-getpointatparam ci (- x 0.1)) (vlax-curve-getpointatparam ci (+ x 0.1))))) params)))
  212.             (if (vl-every (function (lambda ( x ) (inside-p x lw lwi))) pts)
  213.               (setq tst (cons t tst))
  214.               (setq tst (cons nil tst))
  215.             )
  216.           )
  217.           (progn
  218.             (setq pts (apply (function append) (mapcar (function (lambda ( x ) (list (vlax-curve-getpointatparam ci (- x 0.1)) (vlax-curve-getpointatparam ci (+ x 0.1))))) params)))
  219.             (if (vl-every (function (lambda ( x ) (inside-p x lw lwi))) pts)
  220.               (setq tst (cons t tst))
  221.               (setq tst (cons nil tst))
  222.             )
  223.           )
  224.         )
  225.         (if (and ci (not (vlax-erased-p ci)))
  226.           (entdel ci)
  227.         )
  228.       )
  229.     )
  230.     (apply (function and) tst)
  231.   )
  232.  
  233.   (defun chkcircinside ( pp tll / dd )
  234.     (if (and pp tll)
  235.       (if (setq dd (offd pp tll))
  236.         (chkcircinside-dist pp dd)
  237.       )
  238.     )
  239.   )
  240.  
  241.   (defun collinear-pp ( p1 p2 p3 )
  242.     ( (lambda ( a b c )
  243.         (or
  244.           (equal (+ a b) c 1e-8)
  245.           (equal (+ b c) a 1e-8)
  246.           (equal (+ c a) b 1e-8)
  247.         )
  248.       )
  249.       (distance p1 p2) (distance p2 p3) (distance p1 p3)
  250.     )
  251.   )
  252.  
  253.   (defun searchipss ( lst / x y ip d dm ipss )
  254.     (setq ll lst dm 1e+308)
  255.     (if (setq x (car lstt))
  256.       (progn
  257.         (setq ll (vl-remove-if (function (lambda ( z ) (vl-some (function (lambda ( a ) (equal (car z) a 1e-6))) (mapcar (function cadr) lil)))) ll))
  258.         (while (setq y (car ll))
  259.           (setq ll (cdr ll))
  260.           (if
  261.             (and x y
  262.               (setq ip (inters (car x) (polar (car x) (cadr x) 1.0) (car y) (polar (car y) (cadr y) 1.0) nil))
  263.               (not (equal ip (car x) 1e-6))
  264.               (not (equal ip (car y) 1e-6))
  265.             )
  266.             (if (not (equal (setq d (distance ip (caar lil))) 0.0 1e-3))
  267.               (if (and d (< d dm) (chkcircinside ip utl))
  268.                 (setq ipss (list ip x y) dm d)
  269.               )
  270.             )
  271.           )
  272.         )
  273.       )
  274.     )
  275.     ipss
  276.   )
  277.  
  278.   (defun processlil ( t1 t2 ipss angp / n ang ips ip lilx )
  279.     (if (and t1 t2 ipss (not (equal t1 t2 1e-6)))
  280.       (progn
  281.         (if (setq ip (inters (car t1) (cadr t1) (car t2) (cadr t2) nil))
  282.           (setq ang (angle (car ipss) ip))
  283.         )
  284.         (if (equal ang angp 1e-6)
  285.           (progn
  286.             (setq n (length tl))
  287.             (foreach tt tl
  288.               (if
  289.                 (and
  290.                   (vl-some (function (lambda ( x ) (equal x (car tt) 1e-6))) (mapcar (function cadr) lil))
  291.                   (vl-some (function (lambda ( x ) (equal x (cadr tt) 1e-6))) (mapcar (function cadr) lil))
  292.                 )
  293.                 (setq tl (vl-remove tt tl))
  294.               )
  295.             )
  296.             (if (= n (length tl))
  297.               (cond
  298.                 ( (and (vl-some (function (lambda ( x ) (or (equal x (caar tl) 1e-6) (equal x (cadar tl) 1e-6)))) (mapcar (function cadr) lil)) (not (vl-some (function (lambda ( x ) (or (equal x (car (last tl)) 1e-6) (equal x (cadr (last tl)) 1e-6)))) (mapcar (function cadr) lil))))
  299.                   (setq tl (cdr tl))
  300.                 )
  301.                 ( (and (vl-some (function (lambda ( x ) (or (equal x (car (last tl)) 1e-6) (equal x (cadr (last tl)) 1e-6)))) (mapcar (function cadr) lil)) (not (vl-some (function (lambda ( x ) (or (equal x (caar tl) 1e-6) (equal x (cadar tl) 1e-6)))) (mapcar (function cadr) lil))))
  302.                   (setq tl (reverse (cdr (reverse tl))))
  303.                 )
  304.               )
  305.             )
  306.             (if (= n (length tl))
  307.               (setq tl (vl-remove (caddar lstt) tl))
  308.             )
  309.             (setq t1 (car tl))
  310.             (setq t2 (last tl))
  311.             (if (setq ip (inters (car t1) (cadr t1) (car t2) (cadr t2) nil))
  312.               (setq ang (angle (car ipss) ip))
  313.             )
  314.           )
  315.         )
  316.         (setq ips (list (car ipss) ang t1 t2))
  317.         (if ips
  318.           (progn
  319.             (setq lstt (cons ips lstt))
  320.             (foreach p pl
  321.               (if (equal (angle p (car ips)) (cadr (assoc p lst)) 1e-6)
  322.                 (if (not (vl-position (list (car ips) p) lil))
  323.                   (setq lilx (cons (list (car ips) p) lilx))
  324.                 )
  325.               )
  326.             )
  327.             (if (not (vl-position (list (car ips) (caar lil)) lil))
  328.               (setq lilx (cons (list (car ips) (caar lil)) lilx))
  329.             )
  330.           )
  331.         )
  332.       )
  333.     )
  334.     (list lilx ang)
  335.   )
  336.  
  337.   (defun process ( lst att / done flag ll ip x y ips ipss d pos zz dm angp lilxa tt tll t1 t2 k )
  338.     (progn
  339.       (while (not done)
  340.         (setq ips nil)
  341.         (if (not flag)
  342.           (progn
  343.             (setq ll lst ips nil dm 1e+308)
  344.             (while (setq x (car ll))
  345.               (setq ll (cdr ll))
  346.               (setq y (car ll))
  347.               (if (and x y (setq ip (inters (car x) (polar (car x) (cadr x) 1.0) (car y) (polar (car y) (cadr y) 1.0) nil)) (inside-p ip lw lwi))
  348.                 (if (< (setq d (min (distp2t ip (caddr x)) (distp2t ip (last y)))) dm)
  349.                   (setq ips (list ip x y) dm d)
  350.                 )
  351.               )
  352.             )
  353.             (setq pos (vl-position (cadr ips) lst))
  354.             (if pos
  355.               (repeat (numbpos pos (length pl))
  356.                 (setq lst (append (cdr lst) (list (car lst))))
  357.                 (setq tl (append (cdr tl) (list (car tl))))
  358.                 (setq pl (append (cdr pl) (list (car pl))))
  359.                 (setq pli (append (cdr pli) (list (car pli))))
  360.               )
  361.             )
  362.             (setq tt (vl-remove-if (function (lambda ( x ) (= (length (vl-remove-if (function (lambda ( y ) (equal x y 1e-6))) (list (caddr (cadr ips)) (cadddr (cadr ips)) (caddr (caddr ips)) (cadddr (caddr ips))))) 2))) (list (caddr (cadr ips)) (cadddr (cadr ips)) (caddr (caddr ips)) (cadddr (caddr ips)))))
  363.             (setq zz (car (vl-remove-if (function (lambda ( x ) (vl-some (function (lambda ( y ) (equal x y 1e-6))) tt))) (list (caddr (cadr ips)) (cadddr (cadr ips)) (caddr (caddr ips)) (cadddr (caddr ips))))))
  364.             (setq tl (vl-remove-if (function (lambda ( x ) (equal x zz 1e-6))) tl))
  365.             (if (and (car ips) (car (cadr ips)) (car (caddr ips)))
  366.               (cond
  367.                 ( (= att 1)
  368.                   (setq lil (cons (list (car ips) (car (caddr ips))) lil))
  369.                   (setq lil (cons (list (car ips) (car (cadr ips))) lil))
  370.                 )
  371.                 ( t
  372.                   (setq lil (cons (list (car ips) (car (cadr ips))) lil))
  373.                   (setq lil (cons (list (car ips) (car (caddr ips))) lil))
  374.                 )
  375.               )
  376.             )
  377.             (setq ang (angle (car ips) (inters (caar tt) (cadar tt) (caadr tt) (cadadr tt) nil)))
  378.             (setq lstt (cons (list (car ips) ang (car tt) (cadr tt)) lstt))
  379.           )
  380.         )
  381.         (setq flag t)
  382.         (if (not k)
  383.           (setq k 1)
  384.           (setq k (1+ k))
  385.         )
  386.         (if (= k 1)
  387.           (setq tl (vl-remove (caddar lstt) tl))
  388.           (foreach tt tl
  389.             (if
  390.               (and
  391.                 (vl-some (function (lambda ( x ) (equal x (car tt) 1e-6))) (mapcar (function cadr) lil))
  392.                 (vl-some (function (lambda ( x ) (equal x (cadr tt) 1e-6))) (mapcar (function cadr) lil))
  393.               )
  394.               (setq tl (vl-remove tt tl))
  395.             )
  396.           )
  397.         )
  398.         (setq t1 (car tl))
  399.         (setq t2 (last tl))
  400.         (setq ipss nil)
  401.         (setq ipss (searchipss lst))
  402.         (if ipss
  403.           (if (setq lilxa (processlil t1 t2 ipss (cadar lstt)))
  404.             (setq lil (append (car lilxa) lil) ang (cadr lilxa))
  405.           )
  406.         )
  407.         (if
  408.           (or
  409.             (= k (length pl))
  410.             (vl-every (function (lambda ( x ) (vl-some (function (lambda ( y ) (equal x y 1e-6))) (mapcar (function cadr) lil)))) pl)
  411.           )
  412.           (setq done t)
  413.         )
  414.       )
  415.       (unique lil)
  416.     )
  417.   )
  418.  
  419.   (or (and cad doc alo spc) (vl-load))
  420.   (while (= 8 (logand 8 (getvar (quote undoctl))))
  421.     (if (not (vl-cmdf "_.UNDO" "_E"))
  422.       (if doc
  423.         (vla-endundomark doc)
  424.       )
  425.     )
  426.   )
  427.   (if (not (vl-cmdf "_.UNDO" "_BE"))
  428.     (if doc
  429.       (vla-startundomark doc)
  430.     )
  431.   )
  432.   (prompt "\nPick closed polygonal LWPOLYLINE on unlocked layer...")
  433.   (if (setq s (ssget "_+.:E:S:L" (list (cons 0 "LWPOLYLINE") (cons -4 "&=") (cons 70 1) (cons -4 "<not") (cons -4 "<>") (cons 42 0.0) (cons -4 "not>"))))
  434.     (progn
  435.       (setq ti (car (_vl-times)))
  436.       (setq lw (ssname s 0))
  437.       (vlax-invoke (vlax-ename->vla-object lw) (quote offset) -1e-3)
  438.       (setq lwi (entlast))
  439.       (if (> (vlax-curve-getarea lwi) (vlax-curve-getarea lw))
  440.         (progn
  441.           (rlw lw)
  442.           (entdel lwi)
  443.           (vlax-invoke (vlax-ename->vla-object lw) (quote offset) -1e-3)
  444.           (setq lwi (entlast))
  445.         )
  446.       )
  447.       (setq pl (mapcar (function cdr) (vl-remove-if (function (lambda ( x ) (/= (car x) 10))) (entget lw))))
  448.       (setq pli (mapcar (function cdr) (vl-remove-if (function (lambda ( x ) (/= (car x) 10))) (entget lwi))))
  449.       (while (not (equal (car pl) (car pli) 0.1))
  450.         (setq pli (append (cdr pli) (list (car pli))))
  451.       )
  452.       (setq tl (mapcar (function (lambda ( a b ) (list a b))) pl (append (cdr pl) (list (car pl)))))
  453.       (setq utl (unioncollinearplaneprints tl))
  454.       (setq lst (mapcar (function (lambda ( a b c d ) (list a (angle a b) c d))) pl pli (cons (last tl) (reverse (cdr (reverse tl)))) tl))
  455.       (setq llst lst)
  456.       (while (< att 3)
  457.         (if (not att)
  458.           (setq att 1)
  459.           (setq att (1+ att))
  460.         )
  461.         (setq lst llst)
  462.         (setq lil (process lst att))
  463.         (if (= att 1)
  464.           (setq lil1 lil)
  465.           (setq lil2 lil)
  466.         )
  467.       )
  468.       (if (> (length lil1) (length lil2))
  469.         (setq lil lil1)
  470.         (setq lil lil2)
  471.       )
  472.       (foreach li lil
  473.         (if (and (car li) (cadr li) (not (equal (car li) (cadr li) 1e-6)))
  474.           (entmake (list (cons 0 "LINE") (cons 10 (car li)) (cons 11 (cadr li))))
  475.         )
  476.       )
  477.     )
  478.   )
  479.   (*error* nil)
  480. )
  481.  

Regards, stay well and happy (coding)...
M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 03, 2024, 10:21:40 PM
I've compiled *.lsp files packed in .\2D\ribarm\...

*.des files are for BricsCAD
*.VLX files are for AutoCAD

It is actually just slightly faster then before, so I am hoping that someone working with *.arx, *.brx or *.dll will jump in and give it a try for those files that are compiled... 2droof-final.lsp does not need conversion as it's running fast enough that only searching errn, errm combination is unknown, but when it finds it - it's superfast...
Routines packed in *.ZIP you can find here : https://www.theswamp.org/index.php?topic=41837.msg619616#msg619616

So long from me, thanks in advance, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 22, 2024, 11:44:14 AM
I am pushing this topic from bottom to top... I said thanks in advance in hope that someone may come to solution with *.arx, *.brx, or *.dll...

EDIT : Meanwhile, I've removed my poor attempts and left only those things that are good... I've managed to speed up just slightly : roof2d-new-new-new-nn.lsp by changing (if) into (cond) statements and my last one is called : roof2d-new-new-new-nnn.lsp...
So basically I want speeding up even more faster : roof2d-new-new-new-nnn.lsp by using Dot Net or Object Arx... Also I want this if it's possible with : roof2d-new-new-solutions.lsp, but only if it doesn't disturb translator as it's even more complex than roof2d-new-new-new-nnn.lsp

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 29, 2024, 09:19:47 AM
I've run on example which it couldn't solve with previous release... I've fixed it and changed names of main files... Now they are :

- roof2d-new-nn.lsp (if variant)
- roof2d-new-nnn.lsp (cond (= t t) variant)
- roof2d-new-nnnn.lsp (cond t variant)
- roof2d-new-solutions.lsp (cond (= t t) variant)
- roof2d-new-solutions-n.lsp (cond t variant)
Those routines that can solve roof2d-mistake.dwg
And there are also old ones that can solve almost everything else (99%) - except roof2d-mistake.dwg
They start with :
- roof2d-new-new-*.lsp

In archive there you can find *.des and *.VLX of these mentioned *.lsp files...

So long from me,
Regards, stay well and happy...
M.R.

EDIT : Additional *.dwg for testing is posted here : https://www.theswamp.org/index.php?topic=59312.msg619513#msg619513
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on February 29, 2024, 07:08:17 PM
I've updated roof2d*.lsp with just roof2d.lsp, roof2d.des and roof2d.VLX... It should solve everything, but I had to slow it down with testing subs for checking weather solution is good or bad... If it's bad, it restarts and finish as good... That is double timing if even more as it creates duplicate lines which are upon finish OVERKILL-ed...

Regards, M.R.

P.S. Can someone look at it and remove duplication, so that OVERKILL line doesn't need to be neccessary...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on March 03, 2024, 11:01:52 AM
In previous post are my latest revisions, and assuming to testing results posted on DWG here : https://www.theswamp.org/index.php?topic=59312.msg619513#msg619513 , my (cond) version is faster than (if) version, so I'd like that you download *.ZIP in previous post and translate "roof2d-new-nnnn.lsp" and if there are time (it works desiring speed) to translate "roof2d-new-solutions-n.lsp" into adequate *.arx, or *.brx, or *.dll for versions of AutoCAD 2022 and BricsCAD V23...

This all is just my humble request if someone have time or want to earn some reputation here on theswamp.org...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on March 05, 2024, 01:48:35 PM
Change of plan...
As far as I could test my versions, I've found too many lacks... So now request is to work with proved as good and that is chlh_jd's code that is checking errn and errm - loops until it finds correct solution... So we need translation "2droof-final.lsp" into corresponding *.arx, *.brx or *.dll...
You can see where mistakes in calculation with my versions are in this testing DWG and I suggest strongly that you download it as you should have something for checking... Link : https://www.theswamp.org/index.php?topic=59312.msg619513#msg619513

Regards,
M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on March 05, 2024, 03:03:14 PM
I've made slight changes for person who download it lastly - there was 1 download...
Sorry, I can't test and correct so quickly...

M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: kdub_nz on March 05, 2024, 03:51:44 PM
I downloaded the drawing and the code.
Haven't looked at the code.

The times shown on the 'reasonably shaped' profiles is faster than I'd expected.
The times for the complex profiles is still pretty good, in my opinion.

Just how fast should these profiles be solved, in your expectation ?

The average breathing rate for healthy humans is between 12 and 20 breaths per minute
At a mean of 16 that is 3,750 milliseconds per breath.

If a program takes 3 breaths is it slow or is the problem data complicated ?

If it performs 7 times in one breath, is the program fast or is the problem data simple ?

If it fails, does the speed matter ?

Regards,
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: BIGAL on March 05, 2024, 06:43:45 PM
Agree with Kdub_nz what is fast ?

A task for a client 3 hours manual edit, first go 20 minutes, current version 2 minutes for same task, time includes popping Alert messages so user can see its working, 1000+ objects 5 steps. Alerts about 20 seconds apart. Could do acet progress bar but its fast enough.

A typical house is what maybe 8 roof panels max, mine only has 4. Your code would be almost instant at that level.

Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on March 05, 2024, 08:34:57 PM
I am not replying on thoughts in posts, just informing that I uploaded I hope my last revision on "roof2d.lsp", "roof2d.des" and "roof2d.VLX"... If it pass correctly excellent, and if not - I have examples where this is the case - then just relax and leave it as it is... As for me - I know that ObjectArx and .Net are faster than LISP - Vanilla/Visual, so I thought someone may chime in and blow the air around my cave... (Just joking,,,)

Have a nice day,
M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: kdub_nz on March 06, 2024, 12:09:41 AM


I think I'm not making myself understood, but ok, I'll relax and leave it alone.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on March 06, 2024, 09:50:15 AM
I've updated again... Just don't be upset as "roof2d.lsp" is working twicely slower on some complex examples - it finds mistake and try 2nd time with just slightly different change in subfunctions and may throw again at the end solution with mistake(s)... I've also updated testing DWG posted here : https://www.theswamp.org/index.php?topic=59312.msg619513#msg619513

That's all for my report...
M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on March 07, 2024, 12:45:24 PM
I've updated *.ZIP with routines from here : https://www.theswamp.org/index.php?topic=41837.msg619616#msg619616 or https://www.cadtutor.net/forum/files/file/36-hipped-roof-routines/
And updated *.DWG for testing from here : https://www.theswamp.org/index.php?topic=59312.msg619513#msg619513
And I want if someone is reading to translate just "roof2d.lsp" from *.ZIP under .\2d\ribarm\roof2d.lsp... It doesn't matter do you understand what some function is doing, you have to literarily translate every lisp line and see if it's working at the end... I am waiting to see someone's feedback with concrete tryings in makeing *.arx, *.brx or *.dll... I am using AutoCAD 2022 and BricsCAD V23...
I'll be very grateful if it's working faster than *.lsp, *.des, *.VLX... Time is passing and this homework is only for someone dedicated to do things that no one else can - for real programmers... I know for Lee Mac and his dedication to LISP, but unsure for other than that... And futher more if I'd have to choose I'd take fasteast outcome from those 3 (*.arx, *.brx, *.dll)... Gilles is good for *.dll, but is this fastest I don't know... With my checking subfunctions I had to slow down outcome, but those subs are neccessity...

Thanks for reading,
Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on March 08, 2024, 03:40:34 PM
Just wondering...
Why I uploaded *.ZIP and *.DWG when no one downloaded them... peer to peer connection explains why I posted here in attachments, but there is no reason to avoid even you don't want to participate in challenge...
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on March 13, 2024, 12:01:56 PM
Now I don't care which format file is *.lsp, *.des, *.VLX, *.arx, *.brx, *.dll as long as it's faster than my latest revisions in *.lsp file attached in this post...
I hope I don't disturb anyone, just it's that that lisp wasn't good for my work... Now it's amended once again, to satisfy the most of cases, but it turns out that it can still make mistakes, but easily solved manually which satisfies my requirements...
Please haave a look at lisp and try to make it faster in any of those mentioned formats...

Thanks, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on March 13, 2024, 01:42:21 PM
Just saw... If you missed picking polygonal LWPOLYLINE it'll throw an error... Now fixed...

Regards, M.R.
Title: Re: ==={Challenge}===Find the ridge lines of sloped roof
Post by: ribarm on March 14, 2024, 06:30:29 AM
I've chaged a little attached *.lsp to be slightly faster... But I think that's all I can do it for timings... The rest of improvrements lies in hands of someone else... Just I don't know why no one downloaded *.dwg for comparison purposes... Link : https://www.theswamp.org/index.php?topic=59312.msg619513#msg619513

Regards, M.R.