Author Topic: vlr-commandWillStart  (Read 17050 times)

0 Members and 1 Guest are viewing this topic.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: vlr-commandWillStart
« Reply #15 on: March 14, 2011, 08:55:46 PM »
Thanks Alan

I could of written it 'something' like yours, but in my own coding way.  Doing it the way you did it I could have done too but...

I really want to do it via command reactor.

I am trying to learn to program and even though every new adventure I take in programming I get hung up on, I can honestly say every example I gain from these forums I use over and over again and have learned to manipulate the code given to me to my advantage.

I want the code to be loaded when plot is called because it seemed like a good example for me to play with command call reactors.

I also want to be able to run this command from the command line when ever I want as well.

I have many different reasons why I am perticular in why I want to do it my way for issue that plague me on a daly basis.

but my main reason is I just want to learn to program lisp, regarless of easier or more logical ways to do it.

sorry if that was blunt but I read somewhere in the rules that the more straight forward I am the more help I will get.

and Alan I always appriciate your time.

Thanks
Sounds to me like you need to learn a bit more about LISP programming before you start off attempting to tackle reactors. I'm curious, why must you code this as a reactor?
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: vlr-commandWillStart
« Reply #16 on: March 14, 2011, 09:02:46 PM »
Sounds to me like you need to learn a bit more about LISP programming before you start off attempting to tackle reactors. I'm curious, why must you code this as a reactor?

I could be wrong, but it looks as though the OP was predominantly trying to think of a task for which he could attempt to use a reactor to tackle - more, I think, for the experience of using a reactor in LISP than to accomplish the task itself.

But I do agree with the sentiments of you and others - reactors are generally considered an advanced area of LISP, so perhaps not something to tackle as a beginner in LISP.

Lee

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: vlr-commandWillStart
« Reply #17 on: March 15, 2011, 03:49:41 AM »
Hi CadMan

I am learning reactors to, and there are many many things that I don't understand.
And, as a beginer, I think I understand what do you realy need.
So, for your example and to understand how this particular reactor works, please test my code and watch the command line:

Code: [Select]
(vl-load-com)
  (vlr-command-reactor  nil '((:vlr-commandWillStart . PlotSetup))) ; when a command is starting (ANY command), PlotSetup is called, with two arguments

  (defun PlotSetup (Caller CmdSet /)
    (princ (strcat "\nActive command is " (car CmdSet) "!!!\n")) ; just check if my reactor is working
;------- start your code here-----------
;first, test if the command is one that you want, something like (if (equal (car CmdSet) "PLOT") (progn (....
;the rest, is up to you
  )


From VL-Help:
Quote
callback_function is a symbol representing a function to be called when the event fires. Each callback function accepts two arguments:
reactor_object The VLR object that called the callback function.
list A list containing a single element, the string identifying the command.

Your callback_functions is "PlotSetup"
First argument is "Caller" - I dont know how to set it, what data contents and what is good for
Second argument is "CmdSet" and it is a list containing a string, which is the command started, e.g. ("ERASE"); ("PLOT") etc.

Is just a modest start, and I hope we can learn together while the code is developed

cadman6735

  • Guest
Re: vlr-commandWillStart
« Reply #18 on: March 15, 2011, 09:42:37 AM »
Thank you phanaem,
you have given me a great starting point.

One question.  The command psetupin does not work...  Is this the part that I was warned about, I can not use commands in a commandreactor?

-----------------------------------------------------------------


AlanJT
LeeMac is correct, I have no other issue other than wanting to learn reactors.  I am on a learning journy.

I understand you gurus explaining to me that somethings are more advanced than others and I should study more before I tackle a more advanced avenue of Lisp.  My only issue is; I have no clue to what is more advanced than others, untill one of you tell me so.  I find most of this programming to be hard and complicated and it is all advanced stuff to me.  I do know one thing, I try to absorb all that is said to me and do not take it lite when you warn me, I respect your knowledge and experiance.

To me a reactor is just another tool to use, If I am getting ahead of myself and I need to learn more lisp before I can understand reactors, I would not know this till I started my journy with reactors.  I do not know what I do not know and I will never know what I don't know till I find something that requires me to learn something else to master the thing I am interested in.

It is all a journy with no destination other than knowledge.

My hat is off to you gurus, I could not imagine how you 15 plus year users learned some of this stuff without the resource available today.
Thanks to all for all you share

cadman6735

  • Guest
Re: vlr-commandWillStart
« Reply #19 on: March 15, 2011, 06:13:26 PM »
Ok, I finaly got my reactor to work, I am happy with my progress.
I decided to go back to, when the drawing opens to load my plotsetup, so I used vlr-dwg-ractor :vlr-endDwgOpen (why not) and it worked. cool

Now, I am studying AlanJT's code:

Code: [Select]
(defun CES_PageSetups (/)
  (if (findfile "CES-PAGESETUPS.dwg")
    (progn (command "_.psetupin" "CES-PAGESETUPS" "*")
           (while (wcmatch (getvar "cmdnames") "*PSETUPIN*") (command "_y"))
    )
  )
)

This code is what I first wanted to do but I could not figure out how to get past the "this sheet already exists, so do you want to replace it?  y/n"  Which lead me to “hell I will delete the darn things and bring them back in" idea…  Now I see that Alan’s code covers this issue.
I am stuck on
Code: [Select]
(while (wcmatch (getvar "cmdnames") "*PSETUPIN*") (command "_y")) “cmdnames” what is this?

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: vlr-commandWillStart
« Reply #20 on: March 15, 2011, 06:20:22 PM »
Type _.sysvdlg at your AutoCAD command line and you tell us  :wink:

JohnK

  • Administrator
  • Seagull
  • Posts: 10638
Re: vlr-commandWillStart
« Reply #21 on: March 15, 2011, 07:11:25 PM »
I dont have AutoCAD on my laptop so i cant test this (Im doing this blind and from old memory) so be careful (-i.e. dont test on a production drawing) but this should you get started on the path of Reactors nicely (In a gentle manner).

HTH.

Code: [Select]
;;
;; The reactor setup portion of code
(vl-load-reactors)

(vlr-command-reactor nil '((:vlr-CommandWillStart . StartCommand)))

(defun StartCommand (calling-reactor startcommandinfo / thecommandstart)
  (setq thecommandstart (nth 0 startcommandInfo))
  (cond
    ((= thecommandstart "PLOT")
     (psetupin))
    )
  )

;;
;; Support procedures
(defun copy_page-setup (DwgName / psetups $acadver$ $typelib$ $classname$ $dbxDoc$)
  (setq $AcadVer$  (atoi (getvar "acadver"))
        $TypeLib$   (findfile "AxDb.dll")
        $ClassName$ (strcat "ObjectDBX.AxDbDocument" "." (itoa $AcadVer$))
        $dbxdoc$   (vla-GetInterfaceObject (vlax-get-acad-object) $ClassName$) )

  (setq *acadobject*
           (cond
             (*acadobject*)
             ((vlax-get-acad-object)))
        *activedocument*
           (cond
             (*activedocument*)
             ((vla-get-activedocument *acadobject*))) )
 
  (setq psetups (vla-get-plotconfigurations *activedocument*))

  (vla-open $dbxdoc$ DwgName)
 
  (vlax-for x
            (setq dbxdoc-collection (vla-get-plotconfigurations $dbxdoc$))
            (setq name (vla-get-name x))
            (vla-copyobjects
              $dbxdoc$
              (vlax-safearray-fill
                (vlax-make-safearray vlax-vbobject '(0 . 0))
                (list (vla-item dbxdoc-collection name)))
              psetups ) )

  (vlax-release-object $dbxdoc$)
  (princ)
 )

(defun psetupin ( / )
   (setq *acadobject*
            (cond
              (*acadobject*)
              ((vlax-get-acad-object)))
         *activedocument*
            (cond
              (*activedocument*)
              ((vla-get-activedocument *acadobject*))) )
   ;;
   ;; delete existing pagesetups
   (vlax-map-collection (vla-get-plotconfigurations *activedocument*) 'vla-delete)
;;        OR you can also use the following line (they do the same thing)
;;        one is faster or better or something or other.
;; (vlax-for itm (vla-get-plotconfigurations *activedocument*) (vla-delete itm))

   ;;
   ;; import the new ones
   (copy_page-setup "<LOCATION\\TO\\A\\FILE\\WHICH\\HAS\\ALL\\YOUR\\PSETUPS\\DRAWING.DWT")

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

Donate to TheSwamp.org

cadman6735

  • Guest
Re: vlr-commandWillStart
« Reply #22 on: March 16, 2011, 09:35:18 AM »
Thanks Lee

All kinds of treasures and new avenues to explore... 

Quote
"CMDNAMES" = Displays the names of the active and transparent commands. For example, LINE'ZOOM indicates that the ZOOM command is being used transparently during the LINE command.

So 'while' the "CMDNAME" (_.psetupin) is active and iterates thru the existing sheets and the command call is "yes"

Thanks Alan for posting and sharing this code, just goes to show when one is looking for answers for one issue you can find new tricks on the way that can solve other issues.  It's all about the journey.

---------------------------------------------------------------------------------

Thanks se7en for posting your code too.

I will study this to see what tricks you use.  Looks involved.  :-)

BlackBox

  • King Gator
  • Posts: 3770
Re: vlr-commandWillStart
« Reply #23 on: March 16, 2011, 09:40:32 AM »
I dont have AutoCAD on my laptop so i cant test this (Im doing this blind and from old memory) so be careful (-i.e. dont test on a production drawing) but this should you get started on the path of Reactors nicely (In a gentle manner).

HTH.

Code: [Select]
;;
;; The reactor setup portion of code
(vl-load-reactors)

(vlr-command-reactor nil '((:vlr-CommandWillStart . StartCommand)))

(defun StartCommand (calling-reactor startcommandinfo / thecommandstart)
  (setq thecommandstart (nth 0 startcommandInfo))
  (cond
    ((= thecommandstart "PLOT")
     (psetupin))
    )
  )

;;
;; Support procedures
(defun copy_page-setup (DwgName / psetups $acadver$ $typelib$ $classname$ $dbxDoc$)
  (setq $AcadVer$  (atoi (getvar "acadver"))
        $TypeLib$   (findfile "AxDb.dll")
        $ClassName$ (strcat "ObjectDBX.AxDbDocument" "." (itoa $AcadVer$))
        $dbxdoc$   (vla-GetInterfaceObject (vlax-get-acad-object) $ClassName$) )

  (setq *acadobject*
           (cond
             (*acadobject*)
             ((vlax-get-acad-object)))
        *activedocument*
           (cond
             (*activedocument*)
             ((vla-get-activedocument *acadobject*))) )
 
  (setq psetups (vla-get-plotconfigurations *activedocument*))

  (vla-open $dbxdoc$ DwgName)
 
  (vlax-for x
            (setq dbxdoc-collection (vla-get-plotconfigurations $dbxdoc$))
            (setq name (vla-get-name x))
            (vla-copyobjects
              $dbxdoc$
              (vlax-safearray-fill
                (vlax-make-safearray vlax-vbobject '(0 . 0))
                (list (vla-item dbxdoc-collection name)))
              psetups ) )

  (vlax-release-object $dbxdoc$)
  (princ)
 )

(defun psetupin ( / )
   (setq *acadobject*
            (cond
              (*acadobject*)
              ((vlax-get-acad-object)))
         *activedocument*
            (cond
              (*activedocument*)
              ((vla-get-activedocument *acadobject*))) )
   ;;
   ;; delete existing pagesetups
   (vlax-map-collection (vla-get-plotconfigurations *activedocument*) 'vla-delete)
;;        OR you can also use the following line (they do the same thing)
;;        one is faster or better or something or other.
;; (vlax-for itm (vla-get-plotconfigurations *activedocument*) (vla-delete itm))

   ;;
   ;; import the new ones
   (copy_page-setup "<LOCATION\\TO\\A\\FILE\\WHICH\\HAS\\ALL\\YOUR\\PSETUPS\\DRAWING.DWT")

  (princ)
)


Brilliant snippet, Se7en... thanks for sharing.

Could you provide the header attribution you'd like to be saved with this code?

Cheers! :beer:
"How we think determines what we do, and what we do determines what we get."

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: vlr-commandWillStart
« Reply #24 on: March 16, 2011, 09:56:33 AM »
Your method Se7en made me think of this, which may also be an option  :-)

BlackBox

  • King Gator
  • Posts: 3770
Re: vlr-commandWillStart
« Reply #25 on: March 16, 2011, 11:03:11 AM »
How does one successfully delete all named views, after deleting all existing page setups?

Code: [Select]
_$ (setq activeDoc (vla-get-activedocument (vlax-get-acad-object)))
#<VLA-OBJECT IAcadDocument 18ec59d0>

_$ (vlax-for x  (vla-get-plotconfigurations activeDoc) (vla-delete x))
nil

_$ (vlax-for x  (vla-get-views activeDoc) (vla-delete x))
[color=red]; error: Automation Error. Object is referenced by other object(s)[/color]

_$

Note - After deleting all page setups, and prior to deleting all named views, I've set an arbitrary, non-view referencing page setup current <None> via the ._-plot command (not shown).

[edit]
Given that all existing page setups have been deleted, I've tried (vla-refreshplotdeviceinfo (vla-get-layout (vla-get-paperspace activeDoc))) to no avail.
[/edit]

The purpose being that the desired page setups to be imported reference a named view, which has been modified.

If the existing named views are not deleted prior to importing the new page setups, then the new page setup reference the existing named view, as the modified named view is not imported.
"How we think determines what we do, and what we do determines what we get."

JohnK

  • Administrator
  • Seagull
  • Posts: 10638
Re: vlr-commandWillStart
« Reply #26 on: March 16, 2011, 12:51:40 PM »
...
Could you provide the header attribution you'd like to be saved with this code?
...

No attribution for me personally please; I use a screen name (not my real name) because I don't deserve or want any. However I can recommend one for you to use.

How would something like below work?

;;;
;;; PLOT command reactor
;;;
;;; Reacts to the PLOT command
;;;
;;; This program will react to the plot command and
;;; fetch the pagesetups from a template drawing.
;;;
;;; Ex: N/A
;;;
;;; By: Se7en
;;;     03.16.11
;;;
;;; Please consider donating to theSwamp.org
;;;


Your method Se7en made me think of this, which may also be an option  :-)

Lee, Why would my procedure remind you of yours? My routine doesn't have a dialog box and it only allows pagesetups to be copied. Your routine reminds me of one of Dave Stein's routines though...sorta...in a off-shot-backwards kind of way (It is most likely the name you choose for the procedure more than anything). His routine would scan a drawing for something (you would use it via code though) it was a general purpose scan-for-this-block-or-dimstyle-thing written back in 01 or 02 (I don't remember when exactly, but it was about the time I got started with programing). I beat his `scan' routine up and down left and right and it morphed into my own general purpose routine(s). I only got a few "good job!"'s from that "giant" and some of that DBX stuff I did--based on his stuff of course--was one of them. At any rate, I'm getting off topic. My `copy_page-setup' procedure could be converted to a generic (read: crude) "dbx-copy-object" with only minor updates.

***
Oh! I'm not sure but I think I understand what you meant now (because your statement doesnt/didnt make a lot of sense to me right away). My use of DBX reminded you of an alternate method (using a procedure you wrote which uses DBX) for getting the pagesetups into the drawing (I'm sorry, if I misunderstood your point; I am not trying to dismiss it). But your alternate method doesn't use reactors (OPs request) and doesnt happen automatically (Unwritten OP request). What would be the difference between using your procedure and the regular "PSETUPIN" command?

RenderMan,
I have no idea. Try deleting the Views before the pagesetups?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: vlr-commandWillStart
« Reply #27 on: March 16, 2011, 01:03:47 PM »
I shall clarify my earlier sentiment: I meant that your use of creating a DBX Document and proceeding to use the CopyObjects method to deep-clone objects from Collections within that DBX Document to a Collection in the Active Drawing reminded me of the program that I linked to, in which I followed a similar procedure - although generalised to more Collections and Dictionaries within the DBX Document.

I suggested it as an option since it could also import PageSetups (and a lot more besides) and could be called from a reactor call-back function if the OP wished to follow this route.

Lee

JohnK

  • Administrator
  • Seagull
  • Posts: 10638
Re: vlr-commandWillStart
« Reply #28 on: March 16, 2011, 01:32:54 PM »
Oh okay (so my second--and shorter paragraph--would have been enough *lol*). cool.  Im glad i could help add to the OP's option set.

I took a minute and adjusted my `copy_page-setup' routine above to a more generic (read: very crude and purely for the sake of conversation) method. However, if this route were to be considered, i highly recomend beefing up this procedure (I havent looked at Lee's method); there are other things to consider when using DBX (safety precautions namely).

Code: [Select]
(defun dbx-copy (tablename dwgname / $acadver$ $typelib$ $classname$ $dbxDoc$)
  ;; (dbx-copy "PLOTCONFIGURATIONS" "<LOCATION\\TO\\A\\FILE\\WHICH\\HAS\\ALL\\YOUR\\STUFF\\DRAWING.DWT>")
  ;; (dbx-copy "TEXTSTYLES" "<LOCATION\\TO\\A\\FILE\\WHICH\\HAS\\ALL\\YOUR\\STUFF\\DRAWING.DWT>")
  ;; (dbx-copy "DIMSTYLES" "<LOCATION\\TO\\A\\FILE\\WHICH\\HAS\\ALL\\YOUR\\STUFF\\DRAWING.DWT>")
  (setq *acadobject*
        (cond
          (*acadobject*)
          ((vlax-get-acad-object)))
        *activedocument*
        (cond
          (*activedocument*)
          ((vla-get-activedocument *acadobject*))) )

  (setq $AcadVer$  (atoi (getvar "acadver"))
        $TypeLib$   (findfile "AxDb.dll")
        $ClassName$ (strcat "ObjectDBX.AxDbDocument" "." (itoa $AcadVer$))
        $dbxdoc$   (vla-GetInterfaceObject *acadobject* $ClassName$)

        _tableget
                '((tablename object)
                  (cond
                        ( (= (strcase tablename) "BLOCKS")     (vla-get-Blocks     object) )
                        ( (= (strcase tablename) "LAYERS")     (vla-get-Layers     object) )
                        ( (= (strcase tablename) "TEXTSTYLES") (vla-get-textstyles object) )
                        ( (= (strcase tablename) "DIMSTYLES")  (vla-get-dimstyles  object) )
                        ( (= (strcase tablename) "LINETYPES")  (vla-get-linetypes  object) )
                        ( (or
                            (= (strcase tablename) "PLOTCONFIGURATIONS")
                            (= (strcase tablename) "PAGESETUPS")
                            )
                         (vla-get-plotconfigurations object)
                         )
                        ( (= (strcase tablename) "LAYOUTS") (vla-get-Layouts object) )
                        ( (= (strcase tablename) "GROUPS")  (vla-get-Groups  object) )))
        );_ end setq

  (setq tableobjects (_tableget tablename *activedocument*))
  (vla-open $dbxdoc$ DwgName)
 
  (vlax-for x
            (setq dbxdoc-collection (_tableget tablename $dbxdoc$))
            (setq name (vla-get-name x))
            (vla-copyobjects
              $dbxdoc$
              (vlax-safearray-fill
                (vlax-make-safearray vlax-vbobject '(0 . 0))
                (list (vla-item dbxdoc-collection name)))
              tableobjects
              )
            )

  (vlax-release-object $dbxdoc$)
  (princ)
 )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: vlr-commandWillStart
« Reply #29 on: March 16, 2011, 01:46:08 PM »
(I havent looked at Lee's method)

To be honest, I prefer 'version 1.0' of my program - it was so much cleaner - as soon as I started trying to add more and more to it the code became increasingly messy and disjointed.