Author Topic: VBA'er Needs Help  (Read 4019 times)

0 Members and 1 Guest are viewing this topic.

Murphy

  • Guest
VBA'er Needs Help
« on: September 07, 2005, 11:22:07 AM »
I downloaded the following code from the AUGI site, I believe.
Can someone help to add in lines to get the picked text's width factor and apply it to the new line(s) of text?

Code: [Select]
;|
Aptxt.lsp  Written by Dennis Shinn and Don Jacobsen
(c) Seattle AutoCAD User Group

       APTXT.lsp automates the appendage of AutoCAD "DTEXT"
       to an existing line of text. Text attributes of the
       original line of text are used to match the new
       added text.

       This routine was conceived in response to an
       article in the comp.cad.autocad newsgroup requesting
       a method for appending text to an exising paragraph.

AutoCAD is the registered trade mark of Autodesk, Inc.
The Seattle AutoCAD User Group is an independent end
user support organization with no affiliation with
Autodesk other than through the use of the AutoCAD
software.

Permission is hereby granted to distribute this utility provided-
       o- no fee is charged
       o- this copyright notice is included in its entirety

Usual disclaimers apply: this program is guaranteed
to do nothing more than occupy space on your hard
drive provided you are successful in getting it there.
|;

(DeFun c:TN (/ curlay cursty seltxt insp trot valign
halign sty styht just hl vl tlay    twide
       )
  (setvar "cmdecho" 0)
  (graphscr)

 ;SET AND SAVE SYSTEM VARIABLES

  (defun pushvars (varlist / varnames new-value old-value)
    (foreach entry varlist
      (if (setq
    varname   (car entry)
    old-value (getvar varname)
  )
(progn
  (if (setq new-value (cdr entry))
    (setvar varname new-value)
  )
  (setq
    stack  (cons (cons varname old-value) stack)
    cursty (getvar "textstyle")
    curlay (getvar "clayer")
  )
)
      )
    )
  )
  (pushvars
    '(("SNAPMODE" . 0)
     )
  )
  (defun popvars ()
    (foreach entry stack
      (setvar (car entry) (cdr entry))
    )
    (setvar "textstyle" cursty)
    (setvar "clayer" curlay)
    (princ)
  )

 ;ERROR HANDLER

  (defun proto-error (S)
    (if (not (member S '("CONSOLE BREAK" "FUNCTION CANCELED")))
      (PRINC S)
    )
    (command)
    (command)
    (command "UNDO" "END")
    (if UNDOIT
      (command "UNDO" 1)
    )
    (setq *ERROR* old-error)
    (popvars)
  )
  (setq *ERROR* proto-error)
  (command "UNDO" "GROUP")

 ;CODE FOR PROGRAM

  (prompt "\nSelect text line to append new text below ... ")
  (while (not seltxt) (setq seltxt (entsel)))
  (setq
    gettxt (entget (car seltxt))
    sty    (cdr (assoc 7 gettxt))
    halign (cdr (assoc 72 gettxt))
    valign (cdr (assoc 73 gettxt))
    styht  (cdr (assoc 40 (tblsearch "style" sty)))
    trot   (/ (* (cdr (assoc 50 gettxt)) 180.00) PI)
    tlay   (cdr (assoc 8 gettxt))
  )
  (setvar "clayer" tlay)
  (cond
    ((= halign 0) (setq hl "L"))
    ((= halign 1) (setq hl "C"))
    ((= halign 2) (setq hl "R"))
    ((= halign 4) (setq hl "M"))

  )
  (cond
    ((= valign 0) (setq vl ""))
    ((= valign 1) (setq vl "B"))
    ((= valign 2) (setq vl "M"))
    ((= valign 3) (setq vl "T"))
  )
  (if (= halign valign 0)
    (setq insp (cdr (assoc 10 gettxt)))
    (setq insp (cdr (assoc 11 gettxt)))
  )
  (setq just (strcat vl hl))
  (cond
    ((and (> styht 0.0) (= just "L"))
     (command "TEXT" "s" sty insp trot "%%10")
    )
    ((and (> styht 0.0) (/= just "L"))
     (command "TEXT" "s" sty "j" just insp trot "%%10")
    )
    ((and (= styht 0.0) (= just "L"))
     (command "TEXT"
      "s"
      sty
      insp
      (cdr (assoc 40 gettxt))
      trot
      "%%10"
     )
    )
    ((and (= styht 0.0) (/= just "L"))
     (command "TEXT"
      "s"
      sty
      "j"
      just
      insp
      (cdr (assoc 40 gettxt))
      trot
      "%%10"
     )
    )
  )
  (Prompt "\n\nText: ")
  (command "DTEXT" "")
  (princ)

 ;PUT EVERYTHING BACK WERE IT BELONGS

  (command "UNDO" "END")
  (setq *ERROR* old-error)
  (popvars)
)
 ;(prompt "\nTextNext loaded... ")
(princ)


JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: VBA'er Needs Help
« Reply #1 on: September 07, 2005, 11:39:42 AM »
I downloaded the following code from the AUGI site, I believe. ...

Im sorry to hear that. *lol* (W0W?! That code looks like it was cut and pasted together.)

I'll look at the code at lunch Murph.

TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Murphy

  • Guest
Re: VBA'er Needs Help
« Reply #2 on: September 07, 2005, 11:48:50 AM »
Hey, Se8en!!
Long time no see!!

By the way, if you have something better that gets the job done then by all means please post it.
This is what I found to get what I needed. I don't have the time right now or I would make a VBA
equivalent.

Thank you!!

 :-D  :-o  :mrgreen:

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: VBA'er Needs Help
« Reply #3 on: September 07, 2005, 12:10:38 PM »
Yeah it has been a long time. Hows you and miss' doing?

i looked arround some of my files but i couldnt really find anything in the same spirit. All i found was this: (And several diff version of it. But its not really the same thing. ...at all. *lol*)
Code: [Select]
;;;===================================================================;
;;;AdoptText                                                          ;
;;;-------------------------------------------------------------------;
;;; This program will allow you to adopt the text of a selected       ;
;;; to any number of selected objects. This program will not adopt    ;
;;; style or any other properties, it only changes the text string.   ;
;;;                                                                   ;
;;; Author: John Kaul                                                 ;
;;;         06.23.02                                                  ;
;;;===================================================================;

(defun AdoptText (/ Obj-list Obj ent InitalObjects TargetObject
                       TargetObjectText)
  (vl-load-com)
;-----------------------------------
  (defun ErrorTrap (s)
    (princ (strcat "\n>>:Error: " s))
    (term_dialog)
    (if fl (close fl))
    (mapcar
      '(lambda (obj)
(vlax-Release-Object obj)
       )
      Obj-list
    )
    (vlxx-UndoEnd)
    (setq *error* olderr
  olderr  nil)
    (princ)
  )
;-----------------------------------
  (setq olderr *error* *error* ErrorTrap)
  (vlxx-UndoBegin)
  (princ "\n>>:Thank you for chosing Se7en airlines, prepare for takeoff. ")
  (princ "\n>>:Please select entities to be changed:<< ")
  (setq InitalObjects (ssget->vla-list (ssget)))
 
  (if InitalObjects
    (while
      (not
(setq ent (entsel "\n>>:Select the object with the correct text: "))
      )
       (princ "\n--->You Missed, try again.<---")
    )
  )
  P
  (if ent
    (progn
      (setq TargetObject
             (vlax-ename->vla-object (car ent))
            )
     
      (setq TargetObjectText
             (progn
               (if (vlax-property-available-p TargetObject 'TextString)
                 (vlax-get-property TargetObject 'TextString)
                 (vlax-Release-Object TargetObject)
                 )
               )
            )     
     
      (foreach obj InitalObjects
        (progn
          (if (vlax-property-available-p Obj 'TextString)
            (vlax-put-property Obj 'TextString TargetObjectText)
            (vlax-Release-Object Obj)
            )
          )
        )
     
      (vlxx-UndoEnd)
     
      )
    )
 
  (prompt "\n>>:Text adopted, program complete. Thank you for flying Se7en airlines.")
  (princ)
  )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: VBA'er Needs Help
« Reply #4 on: September 07, 2005, 12:19:43 PM »
OT: Dennis Shinn is one of the nicest gentlemen I never met. Many years ago, like late 90's, I wrote a small utility for a problem he posted on comp.cad.autocad and he insisted that he pay me for it because he liked it so much. I declined and declined. In the end he sent me a pound of most excellent Seattle coffee. What a gentleman he is.

/memories

Edit: Holy carp I believe I found a reference to it.
« Last Edit: September 07, 2005, 12:30:20 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: VBA'er Needs Help
« Reply #5 on: September 07, 2005, 12:54:33 PM »
John ;
Dont have time to test that , 'cause I'm going to bed, but from a quick scan it looks like you have a spare 'P' in there somewhere.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: VBA'er Needs Help
« Reply #6 on: September 07, 2005, 01:43:50 PM »
G-night Kerry. Oh no need to test mine; I was just saying that, 'that' code was all that i found in my stuff on the same subject line of "text addition".

Well anywasy Murph, There really isnt a way--perse--to do as you asked with that code, but i do have an idea that just might work. Let me create a test and see if it works, if it does then ill try to add it to yours.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: VBA'er Needs Help
« Reply #7 on: September 07, 2005, 03:07:07 PM »
Well i got a good test. (Sorry it took me so long i got side tracked with a project.)

Ill try to add this to the code you posted soon Murph.

Code: [Select]
(defun c:txttester ( / a b)
 
  (defun tilllastent (a / b)
    (while (setq b (entnext a))
           (entmod (subst (cons 41 0.4) (assoc 41 (entget b)) (entget b)))
           (setq a b)))

  (setq a (entlast))
  ;; record the last ent
  (alert "This is a test. Add some dtext and watch all the text you enter get a new width of 0.4.")
  (command "dtext" (getpoint "\nEnter a point: ") "")
  ;; start the command
  (while (eq (getvar"cmdactive") 1)
    (progn (command pause)))
  ;; wait till the user is done
  (tilllastent a)
  ;; alter all the ent's created while we waited.
)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: VBA'er Needs Help
« Reply #8 on: September 07, 2005, 04:00:48 PM »
Sorry it took so long again Murph. I got super busy this afternoon.

This is real a quick and dirty fix. I didnt alter any of the other code in this proced. (Although he maybe a nice guy, his code needs alot of work.)

Code: [Select]
(DeFun c:TN (/ curlay cursty seltxt insp trot valign
               halign sty styht just hl vl tlay    twide
               )
       (setvar "cmdecho" 0)
       (graphscr)

       ;SET AND SAVE SYSTEM VARIABLES

       (defun pushvars (varlist / varnames new-value old-value)
         (foreach entry varlist
                  (if (setq
                        varname   (car entry)
                        old-value (getvar varname)
                        )
                    (progn
                      (if (setq new-value (cdr entry))
                        (setvar varname new-value)
                        )
                      (setq
                        stack  (cons (cons varname old-value) stack)
                        cursty (getvar "textstyle")
                        curlay (getvar "clayer")
                        )
                      )
                    )
                  )
         )
       (pushvars
         '(("SNAPMODE" . 0)
           )
         )
       (defun popvars ()
         (foreach entry stack
                  (setvar (car entry) (cdr entry))
                  )
         (setvar "textstyle" cursty)
         (setvar "clayer" curlay)
         (princ)
         )

       ;ERROR HANDLER

       (defun proto-error (S)
         (if (not (member S '("CONSOLE BREAK" "FUNCTION CANCELED")))
           (PRINC S)
           )
         (command)
         (command)
         (command "UNDO" "END")
         (if UNDOIT
           (command "UNDO" 1)
           )
         (setq *ERROR* old-error)
         (popvars)
         )
       (setq *ERROR* proto-error)
       (command "UNDO" "GROUP")

       ;CODE FOR PROGRAM

       (prompt "\nSelect text line to append new text below ... ")
       (while (not seltxt) (setq seltxt (entsel)))
       (setq
         gettxt (entget (car seltxt))
         sty    (cdr (assoc 7 gettxt))
         halign (cdr (assoc 72 gettxt))
         valign (cdr (assoc 73 gettxt))
         styht  (cdr (assoc 40 (tblsearch "style" sty)))
         trot   (/ (* (cdr (assoc 50 gettxt)) 180.00) PI)
         tlay   (cdr (assoc 8 gettxt))
         )
       (setvar "clayer" tlay)
       (cond
         ((= halign 0) (setq hl "L"))
         ((= halign 1) (setq hl "C"))
         ((= halign 2) (setq hl "R"))
         ((= halign 4) (setq hl "M"))

         )
       (cond
         ((= valign 0) (setq vl ""))
         ((= valign 1) (setq vl "B"))
         ((= valign 2) (setq vl "M"))
         ((= valign 3) (setq vl "T"))
         )
       (if (= halign valign 0)
         (setq insp (cdr (assoc 10 gettxt)))
         (setq insp (cdr (assoc 11 gettxt)))
         )
       (setq just (strcat vl hl))
       (cond
         ((and (> styht 0.0) (= just "L"))
          (command "TEXT" "s" sty insp trot "%%10")
          )
         ((and (> styht 0.0) (/= just "L"))
          (command "TEXT" "s" sty "j" just insp trot "%%10")
          )
         ((and (= styht 0.0) (= just "L"))
          (command "TEXT"
                   "s"
                   sty
                   insp
                   (cdr (assoc 40 gettxt))
                   trot
                   "%%10"
                   )
          )
         ((and (= styht 0.0) (/= just "L"))
          (command "TEXT"
                   "s"
                   sty
                   "j"
                   just
                   insp
                   (cdr (assoc 40 gettxt))
                   trot
                   "%%10"
                   )
          )
         )
       
       (defun tilllastent (a / b)
         (while (setq b (entnext a))
                (entmod (subst (cons 41 (cdr (assoc 41 (entget (car seltxt))))) (assoc 41 (entget b)) (entget b)))
                (setq a b)))
       (setq a (entlast))

       (Prompt "\n\nText: ")
       (command "DTEXT" "")

       ;; record the last ent
       (while (eq (getvar"cmdactive") 1)
              (progn (command pause)))
       ;; wait till the user is done
       (tilllastent a)
       ;; alter all the ent's created while we waited.
       ;;;  (princ)

       ;PUT EVERYTHING BACK WERE IT BELONGS

       (command "UNDO" "END")
       (setq *ERROR* old-error)
       (popvars)
  )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Murphy

  • Guest
Re: VBA'er Needs Help
« Reply #9 on: September 07, 2005, 06:05:28 PM »
Thank you se8en.
I might just have to break down and learn that weird language.
The only thing I know how to do is to set commands for my toolbar buttons to run my vba stuff.
Makes it much easier to right click the mouse to run the routine again.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: VBA'er Needs Help
« Reply #10 on: September 07, 2005, 10:18:26 PM »
Hey it was my pleasure. Well if you want...we can teach you. (Dont use that app; that needs a bit of a re-write. ...and text entities is kinda a tricky subject to learn from.)   ...If i get some time this week, I'll redo some of that code.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org