Jump to content

Creating online between selected mtext and all mtext in drawing that contain selected mtext


pietrow

Recommended Posts

Hi 

I'm searching for lisp that creates pline between base oint if the selected mtext and all other mtext that contain selected mtext.

For example I clicked mtext "c1" and there are 3 mtext "c1-22" "c1-12" and "c2-13", so the lisp creates two pline, one "c1" - "c1-22" and second "c1" - "c1-12".

Thanks in advance. 

 

 

 

 

Link to comment
Share on other sites

Try this UNTESTED program.

NOTE: Be sure is that your Mtext objects are NOT formatted otherwise another approach with more functions is required to achieve that goal.

(defun c:Test (/ str int sel ent get ins )
  ;;----------------------------------------------------;;
  ;;	Author : Tharwat Al Choufi			;;
  ;; website: https://autolispprograms.wordpress.com	;;
  ;;----------------------------------------------------;;
  (and (setq str (car (entsel "\nSelect text to search for similar content or as prefixed : ")))
       (or (wcmatch (cdr (assoc 0 (setq get (entget str)))) "TEXT,MTEXT")
           (alert "Invalid object selected. Try again")
           )
       (or (setq ins (cdr (assoc 10 get))
                 int -1 sel (ssget "_X" (list '(0 . "TEXT,MTEXT")
                                              (cons 1 (strcat (cdr (assoc 01 get)) "*"))
                                              (cons 410 (getvar 'CTAB)))))
           (alert "Nno similar contents nor as prefixed string found <!>")
           )
       (while (setq int (1+ int) ent (ssname sel int))
         (or (equal str ent)
             (entmake (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 2) '(70 . 0)
                            (cons 10 ins) (cons 10 (cdr (assoc 10 (entget ent))))))
             )
         )
       )
  (princ)
  )
                  

 

  • Thanks 1
Link to comment
Share on other sites

Great I will try this later.

I have a question, is it hard to edit that code to write that longer mtext in the first column and the length of the pline in the second column in the txt file or better - first column the mtext, second - the X component of the pline, third - the Y component of the pline?

Link to comment
Share on other sites

Generally speaking, its very possible so if you can give an example with a DWG then this would clarify the goal of the program and I will see if time serves then will not hesitate to help you for sure.

  • Thanks 1
Link to comment
Share on other sites

I think your magenta polylines are not that accurate since they are not connected in the center of Mtext objects nor they are connected from the insertion point of each Mtext, so is it okay to get the X and Y from the insertion point of Mtexts ? otherwise you need to specify how did you get that base point that you connected from the original text C2 to the other texts. 

  • Thanks 1
Link to comment
Share on other sites

I was modifying the codes to work with the center point of Mtexts along with exporting info to text file. :) 

Try it now and let me know.

(defun c:Test (/     *error*     str   int   sel   ent   get   ins
               xpo   ypo   pos   lst   txt   opn   dim   rtn   rot
               wid
              )
  ;;----------------------------------------------------;;
  ;;	Author : Tharwat Al Choufi			;;
  ;; website: https://autolispprograms.wordpress.com	;;
  ;;----------------------------------------------------;;
  (defun *error* (msg)
    (and dim (setvar 'DIMZIN dim))
    (and opn (close opn))
    (and msg
         (not (wcmatch (strcase msg) "*CANCEL*,*EXIT*,*BREAK*"))
         (princ (strcat "\nError =>: " msg))
    )
    (princ
      "\nThis AutoLISP program was written by Tharwat Al Choufi"
    )
    (princ)
  )
  (and
    (setq str
           (car
             (entsel
               "\nSelect text to search for similar content or as prefixed : "
             )
           )
    )
    (or (wcmatch (cdr (assoc 0 (setq get (entget str))))
                 "TEXT,MTEXT"
        )
        (alert "Invalid object selected. Try again")
    )
    (or (setq ins (assoc 10 get)
              xpo (car (cdr ins))
              ypo (cadr (cdr ins))
              int -1
              sel (ssget "_X"
                         (list '(0 . "TEXT,MTEXT")
                               (cons 1 (strcat (cdr (assoc 01 get)) "*"))
                               (cons 410 (getvar 'CTAB))
                         )
                  )
        )
        (alert
          "Nno similar contents nor as prefixed string found <!>"
        )
    )
    (setq dim (getvar 'DIMZIN))
    (setvar 'DIMZIN 0)
    (setq rot (cdr (assoc 50 get))
          ins (polar (polar (cdr ins) rot (/ (cdr (assoc 42 get)) 2.0))
                     (- rot (* pi 0.5))
                     (/ (cdr (assoc 40 get)) 2.0)
              )
    )
    (while (setq int (1+ int)
                 ent (ssname sel int)
           )
      (or
        (equal str ent)
        (and (setq get (entget ent)
                   pos (assoc 10 get)
                   wid (cdr (assoc 42 get))
                   rot (cdr (assoc 50 get))
             )
             (setq cnt (polar (polar (cdr pos) rot (/ wid 2.0))
                              (- rot (* pi 0.5))
                              (/ (cdr (assoc 40 get)) 2.0)
                       )
             )
             (entmake
               (list '(0 . "LWPOLYLINE")
                     '(100 . "AcDbEntity")
                     '(100 . "AcDbPolyline")
                     '(90 . 2)
                     '(70 . 0)
                     (cons 10 ins)
                     (cons 10
                           (setq pos (polar (polar (cdr pos) rot (/ wid 2.0))
                                            (- rot (* pi 0.5))
                                            (/ (cdr (assoc 40 get)) 2.0)
                                     )
                           )
                     )
               )
             )
             (setq lst (cons (list (cdr (assoc 1 get))
                                   (rtos (abs (- xpo (car pos))) 2 4)
                                   (rtos (abs (- ypo (cadr pos))) 2 4)
                             )
                             lst
                       )
             )
        )
      )
    )
    (setq txt (getfiled "Save as." (getvar 'DWGPREFIX) "txt" 1))
    (setq opn (open txt "w"))
    (write-line "MTEXT\tX\tY" opn)
    (setq
      lst (vl-sort lst (function (lambda (j k) (< (car j) (car k)))))
    )
    (while (setq rtn (car lst))
      (write-line
        (strcat (car rtn) "\t" (cadr rtn) "\t" (caddr rtn))
        opn
      )
      (setq lst (cdr lst))
    )
  )
  (*error* nil)
  (princ)
) (vl-load-com)

 

  • Thanks 1
Link to comment
Share on other sites

  • 3 months later...

Hi Tharwat

 

I try to understand your code and wonder how exactly work "or" and "and" in code above. I read some other post where you were mentioned as person who use this "style" a lot. Only thing I think I fiugred out is that, when you use "or" and several functions inside, then function are executed one by one until one function returns nil.

Could you tell me more about using "or" and "and" not only for conditional tests?

Link to comment
Share on other sites

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...