Jump to content

How to extract text surrounding selected text in a clockwise direction


Recommended Posts

Posted
21 minutes ago, fuccaro said:

PP is my default name for testing. You should add an useful name.

Also as I mentioned before, you should put at least SS in the parameters list

(defun c:ListLenghts ( / ss)
.....
)

 

 

 Done ✅

Posted (edited)

I tend to use short defun names or you can have shortcuts to a long defun name, these are in a Autoload.lsp which is loaded on startup. For me LLL a simple command.

 

(defun c:lll ()(c:listlengths))

 

Some of my defuns are like 47 which sets osnaps to what I like to use, can have L1 L2 etc. I try to stay away from using numbers for say 20 lisps would be like 10, 11 -> 31 hard to remember which number to use. There is plenty of software that use function numbers as shortcuts.

Edited by BIGAL
  • Agree 1
Posted

As I mentioned, I use (defun c:pp()...) while testing, but for finished programs I use descriptive name and good header comments.

  • 2 weeks later...
Posted (edited)

For info, a code I was working on for something else - converts a polyline with arc segments (bulges) to a polyline with short straight lines for the arcs. User can specify the number of degrees along the arc for the line lengths in the code.

 

Putting it in here referring to the (ssget CP ) selection set filters, you can also use this and the mAssoc function to get the points along the polyline to use in the selection set, gives a bit more accuracy? Change vertexin360 to give more or less lines (set at every 2 degrees here which works well for what I need)

 

(defun c:Poly2Chords ( / MyEnt ModifiedEnt)
;;Select Entity
  (setq MyEnt (entget (car (entsel))))     ; Select Polyline / Line Entity 
  (setq ModifiedEnt (LSPoly2Chords MyEnt)) ; Returns modified entity definition list
  (entmake ModifiedEnt)                    ; Here create a new polyline, can also (entmod...) instead
  (princ)
)

(defun LSPoly2Chords ( MyEnt / vertexin360 NewEnt acount p1 p2 open Open b MyBulge MyBulgeC StartAng EndAng MyRadius ccw Chordangle Chords ChordCount NewPt)
;;;;; Sub Functions
  (defun LM:Bulge->Arc ( p1 p2 b / c r ) ;; Refer to Lee Mac Website ;;Gives Arc definition from pline Bulge
    (setq r (/ (* (distance p1 p2) (1+ (* b b))) 4 b)
          c (polar p1 (+ (angle p1 p2) (- (/ pi 2) (* 2 (atan b)))) r)
    )
    (if (minusp b)
        (list c (angle c p2) (angle c p1) (abs r))
        (list c (angle c p1) (angle c p2) (abs r))
    )
  )
  (defun LM:BulgeCenter ( p1 p2 b ) ;; Refer to Lee Mac Website ;;Gives Arc definition from pline Bulge
    (polar p1
        (+ (angle p1 p2) (- (/ pi 2) (* 2 (atan b))))
        (/ (* (distance p1 p2) (1+ (* b b))) 4 b)
    )
  )
  (defun mAssoc ( key lst / result ) ;;Lee Mac: https://www.cadtutor.net/forum/topic/27914-massoc-implementations/
   (foreach x lst
     (if (= key (car x))
       (setq result (cons (cdr x) result))
     )
   )
   (reverse result)
  )
;;;;; End Sub Functions

;;Set Variables
  (setq vertexin360 180)               ; Number of chords in full circle, 360: every 1 degree
  (setq NewEnt (list))                 ; New List for the modified entity definition
  (setq acount 0)                      ; A counter

;;Find curves '42'
  (while (< acount (length MyEnt))
    (if  (and (=  (car (nth acount MyEnt)) 42) ; if dxf code 42
              (/= (cdr (nth acount MyEnt)) 0)  ; and is a value: a bulge!
         ) ; end and
      (progn
        (setq p1 (cdr (nth (- acount 3) MyEnt))) ; Start Coordinate
        (setq p2 (nth (+ acount 2) MyEnt))       ; End Coordinate as dxf code
          (if (= (car p2) 210)                   ; If P2 is "210" and not a "10" - end of polyline
          (if (= (cdr (assoc 70 MyEnt)) 1)
            (progn                               ; Closed Polyline
              (setq p2 (assoc 10 MyEnt))         ; Set end coordinate to start coordinate
              (setq open nil)
            ) ; end progn
            (progn                               ;Open PolyLine, end of polyline
              (setq Open "Open")
            ) ; end progn
          ) ; end if closed / open
        ) ; end if 210
        (setq p2 (cdr p2))                       ; End Coordinate
        (setq b (cdr (nth acount MyEnt)))        ; Bulge
        (if (= Open "Open")                      ; If next point is '210'
          ()                                     ; End of chords
          (progn                                 ; Calculate Chords
            (setq MyBulge (LM:Bulge->Arc p1 p2 b))    ; Bulge as arc
            (setq MyBulgeC (LM:BulgeCenter p1 p2 b) ) ; Bulge Centre
            (setq StartAng (nth 1 MyBulge))           ; start angle centre to point
            (setq EndAng   (nth 2 MyBulge))           ; End angle centre to point
            (setq MyRadius (nth 3 MyBulge))           ; Bulge radius
            (if (< 0 b)(setq ccw 1)(setq ccw -1))     ; clockwise / anticlockwise
            (setq Chordangle (/ (* 4 (atan b))) )
            (setq Chords (* ( /  Chordangle (/ (* 2 pi) vertexin360)) ccw) ) ; point every nth degree

;            (if (> Chords vertexin360) ;;Check number of chords isn't too big: TL, TR 'corners'. Not needed?
;              (progn
;                (setq Chordangle (+ (- (cadr MyBulge) (caddr MyBulge)) (* 1 pi)) )
;                (setq Chords (/  Chordangle (/ (* 2 pi) vertexin360) )) ; point every x degrees
;            )) ; end progn ; end if
            (setq ChordCount 1)

            (while (< ChordCount Chords)
              (if (= ccw 1)
                (setq NewPt (polar MyBulgeC (+ (* (/ (* 2 pi) vertexin360) ChordCount) StartAng) MyRadius) )
                (setq NewPt (polar MyBulgeC (- EndAng (* (/ (* 2 pi) vertexin360) ChordCount) ) MyRadius) )
              )
              (setq NewEnt (append NewEnt (list (cons 42 0)) ) )
              (setq NewEnt (append NewEnt (list (cons 91 0)) ) )
              (setq NewEnt (append NewEnt (list (cons 10 NewPt)) ) )
              (setq NewEnt (append NewEnt (list (cons 40 0)) ) )
              (setq NewEnt (append NewEnt (list (cons 41 0)) ) )
              (setq ChordCount (+ ChordCount 1))
            ) ; end while
          ) ; end progn
        ) ; end if Open (end of line)
      ) ; end progn

      (progn
        (setq NewEnt (append NewEnt (list (nth acount MyEnt)) ) ) ; Add other DXF codes to NewEnt listing
      ) ; end progn
    ) ; end if '42'
    (setq acount (+ acount 1))
  ) ; end while length MyEnt

  (setq NewEnt (subst (cons 90 (length (mAssoc 10 NewEnt))) (assoc 90 NewEnt) NewEnt )) ; update number of verticies
  NewEnt ; return new entity definition
)

 

Edited by Steven P

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...