Looking for Reactor example: Change text value modifies a rectangle.

Discussion in 'AutoCAD' started by Sage Cowsert, Jun 17, 2004.

  1. Sage Cowsert

    Sage Cowsert Guest

    I'm looking for a reactor example where when a user modifes text it changes
    a corrisponding rectangle.

    Example - Text reads 4x6 next to a 4x6 recangle. When the user changes the
    text to 6x8 it would modify to be 6x8.

    Anyone? Thanks. :)
     
    Sage Cowsert, Jun 17, 2004
    #1
  2. Sage Cowsert

    Don Butler Guest

    As I've said before in this group, I'm a newbie too at this, but I believe
    the AutoLisp Tutorial "Garden Path" would help you with this. It is
    available via Help.

    Don
     
    Don Butler, Jun 17, 2004
    #2
  3. Sage Cowsert

    Don Butler Guest

    I agree.

    Reactor implementation can be the key to powerful automated applications.

    Don


     
    Don Butler, Jun 17, 2004
    #3
  4. Sage Cowsert

    andywatson Guest

    Yes! The "Garden Path" tutorial in the AutoLISP tutorial (AutoCAD 2K4) is definitely a good starting point. I have gone through it now on three different occasions, absorbing more on each pass.

    I think the reason you won't find people willing to "teach" this is that there is SO MUCH to teach, at least in a Discussion Group. Sure, you could write a quick and dirty program to do this or that, but there are so many ways a reactor program can turn into a major nightmare.

    You should definitely check out the Garden Path tutorial, and if you don't have the patience to figure it out, you shouldn't be programming with reactors.
     
    andywatson, Jun 17, 2004
    #4
  5. Sage Cowsert

    Rudy Tovar Guest

    Maybe they should just create a newsgroup for just reactors.

    IDEA!

    autodesk.autocad.customization-reactors

    OK, I THOUGHT OF IT FIRST!!!
    --

    AUTODESK
    Authorized Developer
    http://www.Cadentity.com
    MASi



     
    Rudy Tovar, Jun 17, 2004
    #5
  6. Sage Cowsert

    Don Butler Guest

    Coool!

     
    Don Butler, Jun 17, 2004
    #6
  7. Very good Luis Esquivel.
    Alert Luis, error when moving the text

    garcigj
     
    José Luis Garcia Galán, Jun 17, 2004
    #7
  8. Sage Cowsert

    andywatson Guest

    Code:
    ;; initialize function
    ;; load activeX, store and set system variables, error trapping
    (defun initialize ( / )
    (vl-load-com)
    (setq osmode (getvar "OSMODE")
    orthomode (getvar "ORTHOMODE")
    oerror *error*
    acadDoc (vla-get-activedocument (vlax-get-acad-object))
    activeSpace (vla-get-activespace acadDoc)
    acadSpace (if (= activeSpace 1) (vla-get-modelspace acadDoc) (vla-get-paperspace acadDoc))
    *error* myerror)
    (setvar "OSMODE" 0)
    (setvar "ORTHOMODE" 0)
    )
    
    ;; finalize function
    ;; restore system variables, release activeX objects, restore error
    (defun finalize ( / )
    (setvar "OSMODE" osmode)
    (setvar "ORTHOMODE" orthomode)
    (mapcar 'vlax-release-object (list acadSpace acadDoc))
    (setq *error* oerror)
    )
    
    ;; myerror function
    ;; restore environment before ending
    (defun myerror (msg / )
    (princ msg)
    (finalize)
    )
    
    ;; 2d function
    ;; given coordinate list, returns 2d coordinates
    (defun 2d (p / )
    (list (car p) (cadr p))
    )
    
    ;; DrawRectangle function
    ;; given a point, width and height,
    ;; return rectangle lwpolyline object
    (defun DrawRectangle (centerPoint width height / cp returnObj)
    ;; calculate 4 corners of rectangle
    (setq cp (2d centerPoint)
    ul (polar cp (/ PI 2) (/ height 2))
    ul (polar ul PI (/ width 2))
    ur (polar ul 0 width)
    lr (polar ur (/ (* 3 PI) 2) height)
    ll (polar lr PI width)
    ;; create coordinate list in (x1 y1 x2 y2...) format for activex invoking
    lstCoordinates (apply 'append (list ul ur lr ll))
    )
    (setq returnObj (vlax-invoke acadSpace "AddLightweightPolyline" lstCoordinates))
    ;; close lwpolyline
    (vlax-put returnObj "Closed" :vlax-true)
    returnObj
    )
    
    ;; ObjectModified reactor function
    ;; called when dimension text is edited
    ;; receives text object, reactor object, parameter list
    ;; erases old lwpolyline rectangle, draws new rectangle at old centerpoint
    ;; edits reactor data to point to new rectangle
    (defun ObjectModified (notifier-object reactor-object parameter-list)
    ;; if the reactor is still valid (wasn't removed in the ObjectErased reactor function)
    ;; (see ObjectErased reactor function description) then proceed.
    (if (vlr-added-p reactor-object)
    (progn
    (prompt "\nDimension text edited--deleting old rectangle, creating new rectangle, updating reactor object with new handle.")
    (initialize)
    ;; get text object (owner of reactor), new text string, new width & height
    (setq textObj notifier-object
    textString (vlax-get textObj "TextString")
    lstWidthHeight (GetWidthHeight textString))
    ;; get handle and centerpoint stored in AttachReactor function
    (setq lstHandleCenterpoint (vlr-data reactor-object)
    rectangleHandle (car lstHandleCenterpoint)
    rectangleCenterpoint (cadr lstHandleCenterpoint)
    ;; get activex object from handle
    rectangleObj (vla-handletoobject acadDoc rectangleHandle))
    ;; delete old rectangle
    (vla-delete rectangleObj)
    ;; draw new rectangle, get handle
    (setq newRectangleObj (DrawRectangle rectangleCenterpoint (car lstWidthHeight) (cadr lstWidthHeight))
    newRectangleHandle (vlax-get newRectangleObj "Handle"))
    ;; replace old handle with new handle
    (vlr-data-set reactor-object (list newRectangleHandle rectangleCenterpoint))
    (finalize)
    (prompt "\nDone!")
    ); progn
    ); if
    )
    
    ;; ObjectErased reactor function
    ;; called when dimension text is erased
    ;; receives textobject, reactor object, parameter list
    ;; erases lwpolyline rectangle, removes reactor
    ;; Note: Although the reactor is removed in this function, the ObjectModified function will still be called.
    ;;       Since the ObjectModified function will try to erase the lwpolyline rectangle again,
    ;;       I have added an "if" statement (vlr-added-p reactor-object) in the ObjectModified
    ;;       function to test if the reactor is still valid by the time it gets to ObjectModified
    ;;       function.
    (defun ObjectErased (notifier-object reactor-object parameter-list)
    (prompt "\nDimension text erased--removing reactor and associated rectangle.")
    (initialize)
    ;; get handle and centerpoint stored in AttachReactor function
    (setq lstHandleCenterpoint (vlr-data reactor-object)
    rectangleHandle (car lstHandleCenterpoint)
    rectangleObj (vla-handletoobject acadDoc rectangleHandle))
    ;; delete rectangle
    (vla-delete rectangleObj)
    ;; remove reactor
    (vlr-remove reactor-object)
    ;; Note: Although the reactor is removed, ObjectModified reactor has already been fired
    (finalize)
    (prompt "\nDone!")
    )
    
    ;; AttachReactor function
    ;; receives text object, rectangle object and centerpoint
    ;; attaches the rectangle object's handle and centerpoint to the text object reactor
    (defun AttachReactor (tObj rObj p / )
    (setq rHandle (vlax-get rObj "Handle"))
    (vlr-object-reactor (list tObj) (list rHandle p)
    '((:vlr-modified . ObjectModified)
    (:vlr-erased . ObjectErased)
    )
    )
    )
    
    ;; GetWidthHeight function
    ;; receives text string in format "4x8"
    ;; returns width and height in list format (4.0 8.0)
    (defun GetWidthHeight (txt / returnList)
    (setq txt (strcase txt))
    (if (setq xPosition (vl-string-search "X" txt))
    (progn
    (setq width (atof (substr txt 1 xPosition))
    height (atof (substr txt (+ xPosition 2) (- (strlen txt) xPosition)))
    returnList (list width height))
    )
    )
    returnList
    )
    
    ;; Main Program
    ;; asks user to select text entity containing text in format "4x8",
    ;; then prompts user for centerpoint for rectangle using dimensions in text
    ;; attaches an object reactor to text dimension so if dimensions are changed,
    ;; the rectange is updated (old rectangle deleted, new rectangle created)
    (defun RectangleReactor ( / e textObj rectangleObj)
    ;; store/set system & global variables
    (initialize)
    ;; get text entity selection
    (while (null e)
    (setq e (car (entsel "\nSelect text containing rectangle height and width in format \"4x8\" : ")))
    (setq eType (cdr (assoc 0 (entget e))))
    (if (not (or (= eType "TEXT") (= eType "MTEXT")))
    (setq e nil)
    )
    )
    ;; get centerpoint for rectangle
    (setq insPoint (getpoint "\nSelect centerpoint for rectangle: "))
    ;; get activex object from text entity, get text string, convert to width and height
    (setq textObj (vlax-ename->vla-object e)
    textString (vlax-get textObj "TextString")
    lstWidthHeight (GetWidthHeight textString)
    )
    ;; send width and height to DrawRectangle function and get new rectangle object
    (setq rectangleObj (DrawRectangle insPoint (car lstWidthHeight) (cadr lstWidthHeight)))
    ;; send text, rectangle and centerpoint to AttachReactor function
    (AttachReactor textObj rectangleObj insPoint)
    ;; release activex objects
    (mapcar 'vlax-release-object (list textObj rectangleObj))
    ;; restore system variables, release objects
    (finalize)
    (princ)
    )
    
    ;; rr shortcut to RectangleReactor
    (defun rr ( / )
    (RectangleReactor)
    )
    
    ;; prompt user upon loading routine
    (prompt "\nCreate a text entity with rectangle dimensions in the format \"4x8\"")
    (prompt "\nand start RectangleReactor with command \"RR\"")
    

    So, make sure you have text in the drawing, like "20x30", and then run the routine.
    Andrew
     
    andywatson, Jun 17, 2004
    #8
  9. Sage Cowsert

    T.Willey Guest

    Andy,

    Could you attach the file? I want to print it out and learn from it, but when i tried off the web site it cut off some of the text, and when I tried to copy/paste into note pad it came in as like only 4 lines (not with the beautiful spacing you did in your lisp).

    Thanks for posting something we could read and learn from.

    Tim
     
    T.Willey, Jun 17, 2004
    #9
  10. Excuse me, but what is the point in compiling this "example" ?

    Usually, requests for 'examples', are requests for
    example source code, not a 'demo'.



    AutoCAD based Security Planning Solutions:
    http://www.caddzone.com/securityplanning
     
    Tony Tanzillo, Jun 17, 2004
    #10
  11. Sage Cowsert

    Sage Cowsert Guest

    I appreciate everyones help. What I was planning on doing was using this to
    create structural columns. Example user selects HSS4x4x3/16 in a dialog box
    and prompts for a location of the column and text. Then later on if the user
    changes the value to HSS6x6x3/16 the reactor would fire and change the
    column into a 6x6. I've got all the coding done for the column placement and
    text callout. I'm looking for a way to auto'magically' update it in the
    future (reactors). Time to go hit the garden path routine again.
     
    Sage Cowsert, Jun 17, 2004
    #11
  12. Sage Cowsert

    GaryDF Guest

    What about making the reactor persisitant?
    So that it would work the next time the drawing is reopened....
    and still have the rectangle change size.

    Gary
     
    GaryDF, Jun 17, 2004
    #12
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.