Jump to content

How to force running commands in the created drawing ?


Recommended Posts

Posted (edited)

 

How to force running commands in the created drawing ?

 

I created a new drawing using ( vla-add )

The code creates a point in the created drawing but ( zoom extents ) in the original drawing not the created drawing  .. anyone can help in this ?

 

(defun C:DRZ (/ acadApp acadDoc)

  (vl-load-com)
  (setq acadApp (vlax-get-acad-object))
  (setq acadDoc (vla-Add (vla-get-Documents acadApp)))
  (vla-AddPoint (vla-get-ModelSpace acadDoc) (vlax-3d-point 10000 10000 0))
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  (vla-put-ActiveDocument acadApp acadDoc)
  (vla-ZoomExtents acadApp)
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  (vla-Activate acadDoc)
  (vla-SendCommand (vla-get-activedocument (vlax-get-acad-object)) (strcat "Zoom" " " "e" " "))
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  (princ)

)
(princ)

 

 

Sorry for the inconvenience

 

@BIGAL

@Lee Mac

@Nikon

@Steven P

@rlx

Edited by Engineer_Yasser
  • Engineer_Yasser changed the title to How to force running commands in the created drawing ?
Posted

Hi @Engineer_Yasser

If you have created a new drawing from code, to create new objects in it, you will need to assign the vla object name of the new drawing to 'activeDocument'.

Posted
  On 4/4/2025 at 4:53 PM, GLAVCVS said:

Hi @Engineer_Yasser

If you have created a new drawing from code, to create new objects in it, you will need to assign the vla object name of the new drawing to 'activeDocument'.

Expand  

@GLAVCVS  Thanks for replying  ..

 

could you explain by example code please

 

i need to zoom extents in the created drawing

Posted

I just reviewed your code and it works fine for me.
I've even tried creating more objects from your code and it loads them correctly in the new drawing.

Posted (edited)
  On 4/4/2025 at 7:01 PM, GLAVCVS said:

I just reviewed your code and it works fine for me.
I've even tried creating more objects from your code and it loads them correctly in the new drawing.

Expand  

 

@GLAVCVS  The point creation done OK but Zoom Extents never happened

Edited by Engineer_Yasser
Posted

I believe 'vla-zoomExtents' can't work in a drawing that's still being opened and whose parameters AutoCAD hasn't yet calculated. Let's say 'zoomExtents' is too fast here. The closest solution is for your code to calculate the parameters of the display window and apply them from 'vla-zoomWindow'.

Posted
  On 4/4/2025 at 8:21 PM, GLAVCVS said:

I believe 'vla-zoomExtents' can't work in a drawing that's still being opened and whose parameters AutoCAD hasn't yet calculated. Let's say 'zoomExtents' is too fast here. The closest solution is for your code to calculate the parameters of the display window and apply them from 'vla-zoomWindow'.

Expand  

 

@GLAVCVS  Do you have any example code to test your idea ?

Posted

Have you thought about using a script, it can open a new dwg and will automatically then be in that dwg. 

Script code.

(command "New" "Yourtemplatename")
(alert "now in other dwg do your lisp code here")


version 2

(command "New" "Yourtemplatename")
(load "your lisp program")

 

  • Like 1
  • Agree 1
Posted (edited)
  On 4/4/2025 at 10:52 PM, BIGAL said:

Have you thought about using a script, it can open a new dwg and will automatically then be in that dwg. 

Script code.

(command "New" "Yourtemplatename")
(alert "now in other dwg do your lisp code here")


version 2

(command "New" "Yourtemplatename")
(load "your lisp program")

 

Expand  

@BIGAL 

 

I have a lisp code and i want to make zoom extents to the created drawing .. can you ?

Edited by Engineer_Yasser
Posted

Definitely: not possible.
At least with my version of AutoCAD.
Any line of code after the new drawing is created will not be executed in it.
If zoomExtents is so important, you should consider an indirect solution (which I've never tried, but which may be possible): write a LISP in 'acad.lsp' so that it does a 'zoomExtents' when each drawing is opened.

Perhaps someone can share their experience in this regard.

  • Like 1
Posted (edited)

Yes, LIPS is session based, that means you start a command in one drawing, it will run on one drawing and (generally) not run in another.

 

A few ways round this.

As BigAl says a script can cross drawings, open or create a new drawing, run commands in it. 

 

You could adjust the acad.lsp file, this runs on opening / creating a drawing, but your changes will run on every subsequent drawing

 

Expanding acad.lsp file idea, you could add the below to acad.lsp file. Will work on EVERY new drawing.

 

You could create the below as a stand alone LISP file, add it it the startup suit. Will work on EVERY new drawing

 

You could create a temporary LISP file, search for it and if it exists run it (using acad.lsp or a file in the startup suit)... though of course you'd have to consider deleting after running. Will work on EVERY new drawing till temp file is deleted (can do that with LISP once it is loaded delete the file)

 

 

Last one is a bit related, have a list of say, filename prefix and if the file name prefix is in this list do stuff... handy if say a clients drawings are all in the form "12345-dwg-001.dwg", search 123456 and if yes, do stuff. A step further on from the 'if new drawing do stuff' idea above

 

 

All depends how your mind works and which you think is the best solution for you.

 

 

(defun c:testthis ( / SavedDrawing )
  (setq SavedDrawing (getvar "dwgtitled"))

  (if (= SavedDrawing 1)
    (progn
      (alert "Drawing has been saved, is not a new drawing")
    ) ; end progn
    (progn ; savedDrawing = 0
      (alert "Drawing has not been saved, is a new drawing")
    ) ; end progn
  )   ; end if

  (princ)
)
(c:testthis) ;; run on loading

 

Edited by Steven P
  • Like 1
Posted (edited)

This is a way that I have found to do it:

1) Start the LISP that will open the new drawing.

2) In this LISP, set a variable to the Blackboard namespace via (vl-bb-set), alternatively, you could set a value in the registry or a temporary text file.

3) Add a portion to the startup code in "acaddoc.lsp" to check the value of the variable using (vl-bb-ref), and if set, perform the (vla-ZoomExtents).

4) clear the variable previously set with (vl-bb-set) again to set the value to nil.

Edited by pkenewell
  • Like 1
Posted

I didn't look into what your code is doing, but if you use QNEW, focus goes to the new drawing.

 

 

  • Thanks 1
Posted

If achieving zoom extensions is so important and you can't do it any other way, the quickest solution might be to write a DXF and define the initial view in it.

But I suppose you'd have to find a simpler solution.

  • Like 1
Posted
  On 4/8/2025 at 1:12 PM, pkenewell said:

This is a way that I have found to do it:

1) Start the LISP that will open the new drawing.

2) In this LISP, set a variable to the Blackboard namespace via (vl-bb-set), alternatively, you could set a value in the registry or a temporary text file.

3) Add a portion to the startup code in "acaddoc.lsp" to check the value of the variable using (vl-bb-ref), and if set, perform the (vla-ZoomExtents).

4) clear the variable previously set with (vl-bb-set) again to set the value to nil.

Expand  

 

@pkenewell Does this really work?
Have you tried it?

Posted

Yes, it should work - he is setting a flag in the registry, a temporary file or in Blackboard (never used that), when a new file is opened acaddoc.lsp checks for this flag and does stuff, clearing the flag afterwards

Posted (edited)

It doesn't seem easy to find a solution to this. I've tried adding code to AcadDoc.lsp to create reactors. First, I tried using an 'editor-reactor' ('...-endDwgOpen'), and nothing related to the view parameters is executed.
Then I tried using a 'command-reactor' to try to activate it with '(command "_pan"...)', and that doesn't work either.
Nothing seems to be able to modify the initial view until after any startup code has been loaded/executed.

Edited by GLAVCVS
Posted

And I think I just discovered the reason: "EXTMIN" / "EXTMAX" are not the same when the startup code is loaded as they are after the user begins interacting with the drawing.

Somehow, the initial view parameters of the newly created drawing aren't determined until it's made available to the user.

Posted

What makes you believe the Zoom Extents isn't working?

 

With a single Point Object in a drawing, with the point just being a small point, it never centers in the screen. 

 

If you change PDMODE to something like a line or crosshairs, zoom extents works.

Posted
  19 hours ago, SLW210 said:

What makes you believe the Zoom Extents isn't working?

 

With a single Point Object in a drawing, with the point just being a small point, it never centers in the screen. 

 

If you change PDMODE to something like a line or crosshairs, zoom extents works.

Expand  

 

I've added a couple of 'vla-addLine' with distant coordinates to ensure that objects don't accidentally appear on the default screen coordinates
If this doesn't happen to you, I'll have to assume I have a problem with my version of AutoCAD.

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