Author Topic: Lisp to find all text but not working with Block  (Read 1796 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Lisp to find all text but not working with Block
« on: January 30, 2017, 05:26:13 PM »
this lisp to draw a line from matched text to point
The lisp working OK but for block not working well.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:fndtxt ( / A-IP A-STRNG ABLOCKS BLKDEF BLKLIST BLKOBJ
  2.                  CLYR CNB CNT DOC EN I LNE
  3.                  OBJ OBJ-IP OBJ-STRNG OBJ-VL ORGN P1 SS-BLK SS-TXT)
  4.  
  5.  
  6.   (if (and
  7.         (setq orgn (_value "\nSelect Origin attribute/Text : "))
  8.         (setq orgn (vla-get-textstring orgn))
  9.         (setq orgn (LM:UnFormat orgn nil))
  10.         (setq p1 (getpoint "\nPick base point for marks "))
  11.         )
  12.     (progn
  13.       (if (foreach args
  14.                '(
  15.                  ( "TEMP" 8 "Continuous" 0.01 nil 0 "TEMPLATE LAYER")
  16.                  )
  17.         (apply 'lyrmk args)
  18.         )
  19.     (progn
  20.       (setq clyr (getvar "CLAYER"))
  21.       (setvar "CLAYER" "TEMP"))
  22.     (setvar "CLAYER" "Defpoints"))
  23.       (setq cnt 0)
  24.       (setq cnb 0)
  25.       (if (setq ss-TXT (ssget "_X" (list (cons 0 "*TEXT"))))
  26.         (repeat (setq i (sslength ss-TXT))
  27.           (setq obj (ssname ss-TXT cnt))
  28.           (setq obj-vl (vlax-ename->vla-object obj))
  29.           (setq obj-ip (vla-get-insertionpoint obj-vl))
  30.           (setq obj-strng (vla-get-textstring obj-vl))
  31.           (setq obj-strng (LM:UnFormat obj-strng nil))
  32.           (if (eq obj-strng orgn)
  33.             (progn
  34.               (setq Lne (vla-AddLine doc obj-ip (vlax-3d-point p1)))
  35.               (vla-put-layer Lne "TEMP")
  36.               (vlax-put-property Lne "color" 256)
  37.               ))
  38.           (setq cnt (1+ cnt))
  39.         ))
  40.       (if (setq ss-BLK (ssget "_X" (list (cons 0  "INSERT"))))
  41.         (progn
  42.           (setq i (sslength ss-BLK))
  43.           (while (setq en (ssname ss-BLK cnb))
  44.             (setq blkobj (vlax-ename->vla-object en))
  45.             (setq blklist (cons (vla-get-effectivename blkobj) blklist))
  46.           (ssdel en ss-BLK))
  47.           (repeat (length blklist)
  48.             (if (not (vl-catch-all-error-p
  49.                        (setq blkdef (vl-catch-all-apply 'vla-item (list ablocks (nth 0 blklist))))))
  50.               (progn
  51.                 (vlax-for a blkdef
  52.                   (if (eq "ACDBTEXT" (strcase (vla-get-objectname a)))
  53.                     (if (eq orgn (vla-get-textstring a))
  54.                       (progn
  55.                         (setq a-ip (vla-get-insertionpoint a))
  56.                         (setq Lne (vla-AddLine doc obj-ip (vlax-3d-point p1)))
  57.                         (vla-put-layer Lne "TEMP")
  58.                         (vlax-put-property Lne "color" 256)
  59.                         )))
  60.                   8(if (eq "ACDBMTEXT" (strcase (vla-get-objectname a)))
  61.                     (progn
  62.                       (setq a-strng (vla-get-textstring a))
  63.                       (setq a-strng (LM:UnFormat a-strng nil))
  64.                       (if (eq a-strng orgn)
  65.                         (progn
  66.                           (setq a-ip (vla-get-insertionpoint a))
  67.                           (setq Lne (vla-AddLine doc a-ip (vlax-3d-point p1)))
  68.                           (vla-put-layer Lne "TEMP")
  69.                           (vlax-put-property Lne "color" 256)
  70.                           ))
  71.                       ))
  72.                   )))
  73.             (setq blklist (cdr blklist))
  74.             (setq cnt (1+ cnt))
  75.             )
  76.           (command "_attsync" "_N" "*")
  77.           (command "_regenall")
  78.           )
  79.         )
  80.       )
  81.     )  
  82.   )
  83.  
  84. (defun LM:UnFormat ( str mtx / _replace rx ) ;  mtx - T if string is for use in MText - LEE MAC
  85.         (defun _replace ( new old str )
  86.             (vlax-put-property rx 'pattern old)
  87.             (vlax-invoke rx 'replace str new))
  88.         (if (setq rx (vlax-get-or-create-object "VBScript.RegExp"))
  89.             (progn
  90.                 (setq str
  91.                     (vl-catch-all-apply
  92.                         (function
  93.                             (lambda ( )
  94.                                 (vlax-put-property rx 'global     actrue)
  95.                                 (vlax-put-property rx 'multiline  actrue)
  96.                                 (vlax-put-property rx 'ignorecase acfalse)
  97.                                 (foreach pair
  98.                                    '(
  99.                                         ("\032"    . "\\\\\\\\")
  100.                                         (" "       . "\\\\P|\\n|\\t")
  101.                                         ("$1"      . "\\\\(\\\\[ACcFfHLlOopQTW])|\\\\[ACcFfHLlOopQTW][^\\\\;]*;|\\\\[ACcFfHLlOopQTW]")
  102.                                         ("$1$2/$3" . "([^\\\\])\\\\S([^;]*)[/#\\^]([^;]*);")
  103.                                         ("$1$2"    . "\\\\(\\\\S)|[\\\\](})|}")
  104.                                         ("$1"      . "[\\\\]({)|{")
  105.                                     )
  106.                                     (setq str (_replace (car pair) (cdr pair) str))
  107.                                 )
  108.                                 (if mtx
  109.                                     (_replace "\\\\" "\032" (_replace "\\$1$2$3" "(\\\\[ACcFfHLlOoPpQSTW])|({)|(})" str))
  110.                                     (_replace "\\"   "\032" str))))))
  111.                 (vlax-release-object rx)
  112.                 (if (null (vl-catch-all-error-p str)) str))))
  113.  
  114. (defun _value   (str / )
  115.   (while
  116.     (not
  117.       (if (and (setq entt (nentsel str))
  118.                (setq ATTr (car entt))
  119.                (member (vla-get-objectname (setq ATTr (vlax-ename->vla-object ATTr)))
  120.                        '("AcDbAttribute" "AcDbText" "*DIM*" "AcDbMText")))
  121.         ATTr (progn (princ str) nil))))
  122.   ATTr
  123.   )
  124.  
  125. (defun lyrmk (Nme Col lTyp lWgt Plt trns dsc / lay lyrs cmdENTVL LYRDS ) ;lee mac
  126.   ;http://www.cadtutor.net/forum/showthread.php?36882-Check-create-layer-issue-in-Lisp&p=243520&viewfull=1#post243520
  127.  
  128.   (defun lTload (lTyp)
  129.   (or (tblsearch "LTYPE" lTyp)
  130.                 lTyp
  131.                 "acad.lin")))
  132.   (defun mdfy ( / )
  133.                (setq entVL (vlax-ename->vla-object (tblobjname "LAYER" Nme)))
  134.       (and Col (vla-put-Color entVL Col))
  135.       (and lTyp (lTload lTyp) (vla-put-Linetype entVL lTyp))
  136.       (and lWgt (vl-cmdf "_.-layer" "_LWeight" lWgt Nme ""))
  137.       (and (not Plt) (vla-put-Plottable entVL :vlax-false))
  138.       (and (setq LyrDs (vlax-put-property entVL 'Description dsc)))
  139.       (vl-cmdf "_.-layer" "_TR" trns Nme "")
  140.     )
  141.  
  142.   (setq cmd (getvar 'cmdecho))
  143.   (setvar 'cmdecho 0)  
  144.   (if (not (tblsearch "LAYER" Nme))
  145.     (progn
  146.       (setq lay (vla-add lyrs Nme))
  147.       (mdfy))
  148.     (progn
  149.       (mdfy)))
  150.   (setvar 'cmdecho cmd))
  151.  
  152.  
« Last Edit: January 30, 2017, 11:53:48 PM by HasanCAD »

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Lisp to find all text but not working with Block
« Reply #1 on: January 31, 2017, 04:27:38 AM »
Let me explain again, may be there is no good explaination in previous post

Normaly we using FIND command to find similar strings but the strings areselected.
So I am coding a lisp to draw a line to each similar string.
This version working perfect with Text and Mtext.
But with block I am facing some troubles the code draw the line but not in correct location.

Thanks for help
Code - Auto/Visual Lisp: [Select]
  1. (defun c:fndtxt ( / a a-ip a-strng clyr cnb cnt def doc docbl docblk
  2.                  ent-ip ent-strng ent-vl i lne obj obj-ip obj-strng obj-vl orgn
  3.                  p1 s ss-blk ss-txt)
  4.  
  5.        
  6.   (if (and
  7.         (setq orgn (_value "\nSelect Origin attribute/Text : "))
  8.         (setq orgn (vla-get-textstring orgn))
  9.         (setq orgn (LM:UnFormat orgn nil))
  10.         (setq orgn-pnt (getpoint "\nPick base point for Gide Lines: "))
  11.         (setq orgn-pnt (vlax-3d-point orgn-pnt))
  12.         )
  13.     (progn
  14.       (if (foreach args
  15.                         '(
  16.                           ("TEMP" 8 "Continuous" 0.01 nil 0 "TEMPLATE LAYER")
  17.                          )
  18.             (apply 'lyrmk args)
  19.           )
  20.         (progn
  21.           (setq clyr (getvar "CLAYER"))
  22.           (setvar "CLAYER" "TEMP")
  23.         )
  24.         (setvar "CLAYER" "Defpoints")
  25.       )
  26.       (setq cnt 0)
  27.       (setq cnb 0)
  28.       (if (setq ss-TXT (ssget "_X" (list (cons 0 "*TEXT"))))
  29.         (repeat (setq i (sslength ss-TXT))
  30.           (setq obj (ssname ss-TXT cnt))
  31.           (setq obj-vl (vlax-ename->vla-object obj))
  32.           (setq obj-ip (vla-get-insertionpoint obj-vl))
  33.           (setq obj-strng (vla-get-textstring obj-vl))
  34.           (setq obj-strng (LM:UnFormat obj-strng nil))
  35.           (if (eq obj-strng orgn)
  36.             (progn
  37.               (setq Lne (vla-AddLine doc obj-ip orgn-pnt))
  38.               (vla-put-layer Lne "TEMP")
  39.               (vlax-put-property Lne "color" 256)
  40.               ))
  41.           (setq cnt (1+ cnt))
  42.         ))
  43.       (if (setq ss-BLK (ssget "_X" (list (cons 0  "INSERT"))))
  44.         (progn
  45.           (repeat (setq i (sslength ss-BLK))
  46.             (setq efnm (vla-get-effectivename (vlax-ename->vla-object (ssname ss-BLK cnb))))
  47.             (if (not (vl-catch-all-error-p
  48.                      (setq def (vl-catch-all-apply 'vla-item (list docblk efnm)))))
  49.             (vlax-for obj def
  50.               (if (eq "ACDBTEXT" (strcase (vla-get-objectname obj)))
  51.                 (progn
  52.                   (if (eq orgn (vla-get-textstring obj))
  53.                   (progn
  54.                     (setq obj-ipT (vla-get-insertionpoint obj))
  55.                     (setq Lne (vla-AddLine doc obj-ipT orgn-pnt))
  56.                     (vla-put-layer Lne "TEMP")
  57.                     (vlax-put-property Lne "color" 256)
  58.                     ))))
  59.               (if (eq "ACDBMTEXT" (strcase (vla-get-objectname obj)))
  60.                 (progn
  61.                       (setq obj-strng (vla-get-textstring obj))
  62.                       (setq obj-strng (LM:UnFormat obj-strng nil))
  63.                       (if (eq obj-strng orgn)
  64.                         (progn
  65.                           (setq obj-ipMT (vla-get-insertionpoint obj-vl))
  66.                           (setq Lne (vla-AddLine doc obj-ipMT orgn-pnt))
  67.                           (vla-put-layer Lne "TEMP")
  68.                           (vlax-put-property Lne "color" 256)
  69.                           ))))
  70.                 (if (eq "ACDBATTRIBUTEDEFINITION" (strcase (vla-get-objectname obj)))
  71.                 (progn
  72.                       (setq obj-strng (vla-get-textstring obj))
  73.                       (setq obj-strng (LM:UnFormat obj-strng nil))
  74.                       (if (eq obj-strng orgn)
  75.                         (progn
  76.                           (setq obj-ipAT (vla-get-insertionpoint obj-vl))
  77.                           (setq Lne (vla-AddLine doc obj-ipAT orgn-pnt))
  78.                           (vla-put-layer Lne "TEMP")
  79.                           (vlax-put-property Lne "color" 256)
  80.                           ))))
  81.               )
  82.             )      
  83.             (setq cnb (1+ cnb))
  84.             ))))))
  85.    
  86.  
  87. (defun _replace ( new old str )
  88.             (vlax-put-property rx 'pattern old)
  89.             (vlax-invoke rx 'replace str new))
  90.  
  91. (defun LM:UnFormat ( str mtx / _replace rx );  mtx - T if string is for use in MText - LEE MAC
  92.        
  93.     (defun _replace ( new old str )
  94.         (vlax-put-property rx 'pattern old)
  95.         (vlax-invoke rx 'replace str new)
  96.     )
  97.     (if (setq rx (vlax-get-or-create-object "VBScript.RegExp"))
  98.         (progn
  99.             (setq str
  100.                 (vl-catch-all-apply
  101.                     (function
  102.                         (lambda ( )
  103.                             (vlax-put-property rx 'global     actrue)
  104.                             (vlax-put-property rx 'multiline  actrue)
  105.                             (vlax-put-property rx 'ignorecase acfalse)
  106.                             (foreach pair
  107.                                '(
  108.                                     ("\032"    . "\\\\\\\\")
  109.                                     (" "       . "\\\\P|\\n|\\t")
  110.                                     ("$1"      . "\\\\(\\\\[ACcFfHLlOopQTW])|\\\\[ACcFfHLlOopQTW][^\\\\;]*;|\\\\[ACcFfHLlOopQTW]")
  111.                                     ("$1$2/$3" . "([^\\\\])\\\\S([^;]*)[/#\\^]([^;]*);")
  112.                                     ("$1$2"    . "\\\\(\\\\S)|[\\\\](})|}")
  113.                                     ("$1"      . "[\\\\]({)|{")
  114.                                 )
  115.                                 (setq str (_replace (car pair) (cdr pair) str))
  116.                             )
  117.                             (if mtx
  118.                                 (_replace "\\\\" "\032" (_replace "\\$1$2$3" "(\\\\[ACcFfHLlOoPpQSTW])|({)|(})" str))
  119.                                 (_replace "\\"   "\032" str)
  120.                             )
  121.                         )
  122.                     )
  123.                 )
  124.             )
  125.             (vlax-release-object rx)
  126.             (if (null (vl-catch-all-error-p str))
  127.                 str
  128.             )
  129.         )
  130.     )
  131. )
  132.  
  133. (defun _value   (str / )
  134.   (while
  135.     (not
  136.       (if (and (setq entt (nentsel str))
  137.                (setq ATTr (car entt))
  138.                (member (vla-get-objectname (setq ATTr (vlax-ename->vla-object ATTr)))
  139.                        '("AcDbAttribute" "AcDbText" "*DIM*" "AcDbMText")))
  140.         ATTr (progn (princ str) nil))))
  141.   ATTr
  142.   )
  143.  
  144. (defun lyrmk (Nme Col lTyp lWgt Plt trns dsc / lay lyrs cmdENTVL LYRDS ) ;lee mac
  145.   ;http://www.cadtutor.net/forum/showthread.php?36882-Check-create-layer-issue-in-Lisp&p=243520&viewfull=1#post243520
  146.    
  147.   (setq cmd (getvar 'cmdecho))
  148.   (setvar 'cmdecho 0)  
  149.   (if (not (tblsearch "LAYER" Nme))
  150.     (progn
  151.       (setq lay (vla-add lyrs Nme))
  152.       (mdfy))
  153.     (progn
  154.       (mdfy)))
  155.   (setvar 'cmdecho cmd))
  156.  
  157. (defun lTload (lTyp)
  158.   (or (tblsearch "LTYPE" lTyp)
  159.                 lTyp
  160.                 "acad.lin")))
  161.  
  162. (defun mdfy ( / )
  163.                (setq entVL (vlax-ename->vla-object (tblobjname "LAYER" Nme)))
  164.       (and Col (vla-put-Color entVL Col))
  165.       (and lTyp (lTload lTyp) (vla-put-Linetype entVL lTyp))
  166.       (and lWgt (vl-cmdf "_.-layer" "_LWeight" lWgt Nme ""))
  167.       (and (not Plt) (vla-put-Plottable entVL :vlax-false))
  168.       (and (setq LyrDs (vlax-put-property entVL 'Description dsc)))
  169.       (vl-cmdf "_.-layer" "_TR" trns Nme ""))

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: Lisp to find all text but not working with Block
« Reply #2 on: January 31, 2017, 05:52:09 AM »

Quote
This version working perfect with Text and Mtext.
But with block I am facing some troubles the code draw the line but not in correct location.

The issue can be resolved by refining the problem declaration.

Discover where the line is currently being drawn to/from ... then adjust your problem description.



Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Lisp to find all text but not working with Block
« Reply #3 on: January 31, 2017, 07:58:18 AM »
- Lisp detect TEXT and MTEXT
- Lisp does not detect TEXT and MTEXT in block
- Lisp does not detect attribute block
I belive there is a mistake in getting insertion point for the object.
Should we get the coordinate for the object add to the add to coordinate of block ?

Thanks

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Lisp to find all text but not working with Block
« Reply #4 on: January 31, 2017, 12:30:23 PM »
The coordinate data obtained from the block definition will need to be transformed relative to each block reference which references the definition; for this, you will require a function similar to gile's RefGeom function.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Lisp to find all text but not working with Block
« Reply #5 on: January 31, 2017, 05:37:36 PM »
The coordinate data obtained from the block definition will need to be transformed relative to each block reference which references the definition; for this, you will require a function similar to gile's RefGeom function.
I tried to add this functuon but In fact I stucked at this point
Could some one help me at this point

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: Lisp to find all text but not working with Block
« Reply #6 on: January 31, 2017, 06:19:14 PM »
Can you please post the drawing you are using.

I'll have a look tonight when I get home if no-one else pops in first.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Lisp to find all text but not working with Block
« Reply #7 on: February 01, 2017, 02:22:18 AM »
Can you please post the drawing you are using.

I'll have a look tonight when I get home if no-one else pops in first.
Thanks Kerry for your valuable help

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Lisp to find all text but not working with Block
« Reply #8 on: February 02, 2017, 11:10:45 PM »
up