Jump to content

returning focus to Excel after lisp ends


Tomislav

Recommended Posts

Posted (edited)

I do remember BricsCAD execute lisp code to other drawings that where already open.

 

--edit

found the post but it didn't work in AutoCAD back then either.

 

;;----------------------------------------------------------------------------;;
;; Copy Selection to All open Drawings works in BrisCAD
;; https://www.cadtutor.net/forum/topic/86226-returning-focus-to-excel-after-lisp-ends/
(defun C:Copy_to_All_Drawings () (C:C2AD))
(defun C:C2AD (/ ss x)
  (vl-load-com)
  (setq ss (ssget))
  (vl-cmdf "_.Copybase" "_non" "0,0" SS "")
  (vlax-for x (vla-get-documents (vlax-get-acad-object))
    (vla-SendCommand x (strcat "_.pasteclip pause")) 
  )
  (princ)
)

 

Edited by mhupp
  • Like 1
Link to comment
Share on other sites

probably not exactly what you want but should be relatively easy to modify to copy to all open drawings

 

;;; copy to drawing
(defun ctd ( ss dwg / ss->ol dbx_ver acApp acDoc dbx object-list object-safe-array)  
  (defun SS->OL (ss / i l)
    (setq i 0)(repeat (sslength ss)(setq l (cons (vlax-ename->vla-object (ssname ss i)) l) i (1+ i))) l)
  (defun dbx_ver ( / v)
    (strcat "objectdbx.axdbdocument" (if (< (setq v (atoi (getvar 'acadver))) 16) "" (strcat "." (itoa v)))))
  (setq acApp (vlax-get-acad-object) acDoc (vla-get-ActiveDocument acApp))
  (setq dbx (vl-catch-all-apply 'vla-getinterfaceobject (list acApp (dbx_ver))))
  (vla-open dbx dwg)
  ; put all block objects in a list
  (foreach object (ss->ol ss) (setq object-list (cons object object-list)))
  ; put list with objects in a safe array
  (setq object-safe-array (vlax-make-safearray vlax-vbobject (cons 0 (1- (length object-list)))))
  (vl-catch-all-apply 'vlax-safearray-fill (list object-safe-array object-list))
  ; copy objects to wblock-dbx-container
  (vla-CopyObjects acDoc object-safe-array (vla-get-ModelSpace dbx))
  (vl-catch-all-apply 'vla-saveas (list dbx dwg))
  (vl-catch-all-apply 'vlax-release-object (list dbx))
  (setq object-list nil object-safe-array nil)
  (princ)
)

(defun c:t1 ( / ss dwg )
  (if (and (setq ss (ssget))(setq dwg (getfiled "Copy objects to :" (getvar 'dwgprefix) "dwg" 0)))(ctd ss dwg))(princ))

 

maybe :

;;; copy to all open doc
(defun ctaod ( / ss  ss->ol dbx_ver acApp acDoc dbx object-list object-safe-array)  
  (defun SS->OL (ss / i l)
    (setq i 0)(repeat (sslength ss)(setq l (cons (vlax-ename->vla-object (ssname ss i)) l) i (1+ i))) l)
  (defun dbx_ver ( / v)
    (strcat "objectdbx.axdbdocument" (if (< (setq v (atoi (getvar 'acadver))) 16) "" (strcat "." (itoa v)))))
  (setq acApp (vlax-get-acad-object) acDoc (vla-get-ActiveDocument acApp))
  ;;; select objects
  (setq ss (ssget))
  ; put all objects in a list
  (foreach object (ss->ol ss) (setq object-list (cons object object-list)))
  ; put list with objects in a safe array
  (setq object-safe-array (vlax-make-safearray vlax-vbobject (cons 0 (1- (length object-list)))))
  (vl-catch-all-apply 'vlax-safearray-fill (list object-safe-array object-list))
  ;;; copy objects to all open docs except active doc
  (vlax-for x (vla-get-documents (vlax-get-acad-object))
    (if (not (eq x acDoc))
      (progn
	(vla-CopyObjects acDoc object-safe-array (vla-get-ModelSpace x))
	(vla-save x)
      )
    )
  )
  (princ)
)

 

 

Edited by rlx
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

thank you very much, although i'm not familiar with part of instructions used , i can see it will be useful although my goal is to copy ss to only one newly created drawing in every pass of one big loop, so i will try to modify it accordingly to accept name for new file and create it, and then copy to it, save it and close it...

Link to comment
Share on other sites

;;; copy to new doc
(defun ctnd ( ss new-dwg-name / SS->OL dbx_ver acApp acDoc dbx object object-list object-safe-array)
  (defun SS->OL (ss / i l)
    (setq i 0)(repeat (sslength ss)(setq l (cons (vlax-ename->vla-object (ssname ss i)) l) i (1+ i))) l)
  (defun dbx_ver ( / v)
    (strcat "objectdbx.axdbdocument" (if (< (setq v (atoi (getvar 'acadver))) 16) "" (strcat "." (itoa v)))))
  (setq acApp (vlax-get-acad-object) acDoc (vla-get-ActiveDocument acApp))
  (setq dbx (vl-catch-all-apply 'vla-getinterfaceobject (list (vlax-get-acad-object) (dbx_ver))))
  (vl-catch-all-apply 'vla-saveas (list dbx new-dwg-name))
  ; put all objects in a list
  (foreach object (ss->ol ss) (setq object-list (cons object object-list)))
  ; put list with objects in a safe array
  (setq object-safe-array (vlax-make-safearray vlax-vbobject (cons 0 (1- (length object-list)))))
  (vl-catch-all-apply 'vlax-safearray-fill (list object-safe-array object-list))
  ; copy objects to wblock-dbx-container
  (vla-CopyObjects acDoc object-safe-array (vla-get-ModelSpace dbx))
  (vl-catch-all-apply 'vla-saveas (list dbx new-dwg-name))
  (vl-catch-all-apply 'vlax-release-object (list dbx))
  (setq object-list nil object-safe-array nil)
  (princ)
)


(defun c:t2 ( / ss new-dwg )
  (if (and (setq ss (ssget))(setq new-dwg (getfiled "Copy objects to :" (getvar 'dwgprefix) "dwg" 1)))
    (ctnd ss new-dwg)
  )
  (princ)
)

 

  • Thanks 1
Link to comment
Share on other sites

Posted (edited)

well, what to say, you're too fast 😃👍 that is it THANX...will name it accordingly rlx:ctnd

Edited by Tomislav
Link to comment
Share on other sites

You're welcome (although I feel I just created lisp version for the wblock command 🤓 )

 

🐉

  • Funny 1
Link to comment
Share on other sites

Posted (edited)

would you believe me, this is first time i've heard of that command ☺️, always using block/insert...

one line of code gives me error:   

Automation Error. Invalid owner object;

this one

(vla-CopyObjects acDoc object-safe-array (vla-get-ModelSpace dbx))

  

Edited by Tomislav
Link to comment
Share on other sites

dont have this error myself... do you use (vl-load-com) in your startup?

 

but wblock alternative :

 

(defun c:t3 ( / ss fn)
  (setq ss (ssget) fn "c:/temp/rlxie.dwg")
  (setvar 'filedia 0)
  (command "-wblock" fn "" "0,0" ss "" "oops")
  (setvar 'filedia 1)
  (princ)
)

 

loose the "oops" if objects must be deleted from source drawing

Link to comment
Share on other sites

yes, specifically error comes from (vla-get-ModelSpace dbx)

this, wblock is really nice short one

 

Link to comment
Share on other sites

Posted (edited)

what is interesting, your ctnd works on its own, but inserted in my lisp gives mentioned error, so i'm really confused there...

could it be my ss includes objects that can't be copied, so i should filter it or something?

Edited by Tomislav
Link to comment
Share on other sites

cant reproduce your error , all sunny on my side

maybe change last line to (setq object-list nil object-safe-array nil dbx nil)

or set some debug points in your lisp editor and see if for example object-list / safe-array is created correctly

else use wblock , maybe not high-tech but if it gets the job done, who cares.

 

well I'm of to site for a couple of hours now...

 

ps. and check if your filename has no invalid characters in it

Edited by rlx
  • Like 1
Link to comment
Share on other sites

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