Vla-open and execute command fails

Vla-open and execute command fails

DGRL
Advisor Advisor
1,950 Views
7 Replies
Message 1 of 8

Vla-open and execute command fails

DGRL
Advisor
Advisor

Dear coders,

 

I open dwg's using the vla-open like this

 

  (foreach & Files	
  (setq File (vla-open (vla-get-documents (vlax-get-acad-object)) &))
	(c:mycommand)	
	;(vla-save File)
	;(vla-close File)
	(vlax-release-object File)
  )

All is working fine except the (c:mycommand)
it is not even loaded on the dwg's that are being opened while the vla-save is working

 

Is it possible to execute external lsp while using vla-open??

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
1,951 Views
7 Replies
Replies (7)
Message 2 of 8

Satish_Rajdev
Advocate
Advocate

I think there are 2 options do this.

 

1. Use vla-sendcommand function in your current code. More details

 

2. Perform functions in current drawing and then send results to other drawing using vla-copyobjects

Example : I'm using this for 1 of my program

(defun copy:object (sourcedocument targetdocument object / a)
  (if (setq a (vla-copyobjects
		sourcedocument
		(vlax-safearray-fill
		  (vlax-make-safearray vlax-vbobject '(0 . 0))
		  (list object)
		)
		(vla-get-modelspace targetdocument)
	      )
      )
    (car (vlax-safearray->list (vlax-variant-value a)))
  )
)

Best Regards,
Satish Rajdev


REY Technologies | Linked IN | YouTube Channel


 

0 Likes
Message 3 of 8

DGRL
Advisor
Advisor

HI

 

Thanks for your reply

I will try option 1 because I fill in data that is not present in the current dwg  (like Attributes)

 

 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 4 of 8

Satish_Rajdev
Advocate
Advocate
Can you please post that code here? So that we can get idea of it

Best Regards,
Satish Rajdev


REY Technologies | Linked IN | YouTube Channel


 

0 Likes
Message 5 of 8

DGRL
Advisor
Advisor

The red bols line is giving back parameter not optional

 

 

;;;CADALYST 03/05 Tip2023: PurgeFiles.lsp	Directory Clean Up	(c) Andrzej Gumula


;;; [c]2004 Andrzej Gumula, Katowice, Poland
;;; e-mail: a.gumula@wp.pl
;;; This routine purge dwg files from selected folder


(vl-load-com) (defun c:PurgeAuditFiles (/ FilesList DwgPath SubDir Files File) (defun GetFolder (/ Dir Item Path) (cond ((setq Dir (vlax-invoke (vlax-get-or-create-object "Shell.Application") 'browseforfolder 0 "Select folder with DWG files:" 1 "")) (cond ((not (vl-catch-all-error-p (vl-catch-all-apply 'vlax-invoke-method (list Dir 'Items)))) (setq Item (vlax-invoke-method (vlax-invoke-method Dir 'Items) 'Item)) (setq Path (vla-get-path Item)) (if (not (member (substr Path (strlen Path) 1) (list "/" "\\"))) (setq Path (strcat Path "\\")) );end if ) );end cond ) );end cond Path );end GetFolder (defun vl-findfile (Location / DirList Path AllPath) (MakeDirList Location) (setq DirList (cons Location DirList)) (foreach Elem DirList (if (setq Path (vl-directory-files Elem "*.dwg")) (foreach Item Path (setq AllPath (cons (strcat Elem "/" Item) AllPath))) );end if ) (reverse AllPath) );end vl-findfile (defun MakeDirList (Arg / TmpList) (setq TmpList (cddr (vl-directory-files Arg nil -1))) (cond (TmpList (setq DirList (append DirList (mapcar '(lambda (z) (strcat Arg "/" z)) TmpList))) (foreach Item TmpList (MakeDirList (strcat Arg "/" Item))) ) );end cond );end MakeDirList (if (not FileSystemObject) (setq FileSystemObject (vla-getInterfaceObject (vlax-get-acad-object) "Scripting.FileSystemObject")) );end if (cond ((= (getvar "SDI") 0) (cond ((setq DwgPath (GetFolder)) (initget "Yes No") (setq Subdir (cond ((getkword "\nLooking for subfolders? No,[Yes]: ")) (T "Yes"))) (if (equal SubDir "Yes") (setq Files (vl-findfile (substr DwgPath 1 (1- (strlen DwgPath))))) (setq Files (mapcar '(lambda (x) (strcat dwgpath x))(vl-directory-files DwgPath "*.dwg" 1))) );end if (setq Files (mapcar 'strcase Files)) (cond (Files (vlax-for & (vla-get-documents (vlax-get-acad-object )) (setq FilesList (cons (strcase (vla-get-fullname &)) FilesList))) (foreach & Files (cond ((not (member & FilesList )) (cond ((/= (logand (vlax-get-property (vlax-invoke-method FileSystemObject 'getfile &) 'Attributes) 1) 1) (cond ((setq File (vla-open (vla-get-documents (vlax-get-acad-object)) &)) (prompt (strcat "\nPurge " & ". Please wait...")) (vla-SendCommand File (c:MYCOMMAND)) <-- gives back ActiveX Server returned an error: Parameter not optional ;(vla-purgeall File) ; (vla-AuditInfo File T) (prompt (strcat "\nSave and close " &)) (vla-save File) (vla-close File) (vlax-release-object File) ) (T (prompt (strcat "\nCannot open " & "\nDrawing file was created by an incompatible version. "))) );end cond ) (T (prompt (strcat & " is read-only. Purge canceled. "))) );end cond ) (T (prompt (strcat & " is open now. Purge canceled. "))) );end cond );end foreach ) (T (prompt "\nNothing files found to purge. ")) );end cond ) (T (prompt "\nNothing selected. ")) );end cond ) (T (prompt "\nThe routine is not available in SDI mode. ")) );end cond (princ) );end c:PurgeFile (prompt "\nLoaded new command PurgeFiles. ") (prompt "\n[c]2004 Andrzej Gumula. ") (princ)
If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 6 of 8

Satish_Rajdev
Advocate
Advocate

This is not the way to add vla-sendcommand function. Can you post your code c:mycommand?

Best Regards,
Satish Rajdev


REY Technologies | Linked IN | YouTube Channel


 

0 Likes
Message 7 of 8

_gile
Consultant
Consultant

Hi,

 

Keep in mind LISP execution only work in document context.

IOW, a LISP expression/function started in Drawing1 can not continue in another drawing.

 

For example, Drawing1 is the active document in which you execute:

 

(setq doc (vla-Open (vla-get-documents (vlax-get-acad-object)) "C:\\Temp\\Test.dwg"))
(vla-SendCommand doc "_circle 0,0 10 ")

It will open Test.dwg (if found) but it draws the circle in Drawing1.

 

That's to say you cannot use (command ...) or (vla-SendCommand ...) in another drawing,

 

 

But you can use other vla* functions to access to the newly opened document.

 

(setq doc (vla-Open (vla-get-documents (vlax-get-acad-object)) "C:\\Temp\\Test.dwg"))
(vla-AddCircle (vla-get-ModelSpace doc) (vlax-3d-point '(0. 0. 0.)) 10.)

This way, the circle will be drawn in Test.dwg.

 

 

So, if you can build a new function with the behavior of 'c:mycommand ' which accepts a document (vla-object) as argument and which only uses vla functions to edit the document (or its properties), you should be able to run it in a newly opened document.

You could even do better; open the drawing "in memory" only (that's to say not in the editor) with ObjectDBX, this runs much more faster.

 

Always with the same example:

 

(defun gc:GetAxDbDoc (filename / majVer progId axdbdoc)
  (vl-load-com)
  (setq progId
         (if (< (setq majVer (substr (getvar 'acadver) 1 2)) "16")
           "ObjectDBX.AxDbDocument"
           (strcat "ObjectDBX.AxDbDocument." majVer)
         )
  )
  (if (setq axdbdoc (vlax-create-object progId))
    (if (vl-catch-all-apply
          'vla-open
          (list axdbdoc filename)
        )
      (not (vlax-release-object axdbdoc))
      axdbdoc
    )
  )
)

(if (setq doc (gc:GetAxDbDoc "C:\\Temp\\Test.dwg"))
  (progn
    (vla-AddCircle (vla-get-ModelSpace doc) (vlax-3d-point '(0. 0. 0.)) 10.)
    (vla-SaveAs doc "C:\\Temp\\Test.dwg")
    (vlax-release-object doc)
  )
)

This way the circle is drawn in Test.dwg even the file heven't been open in the AutoCAD editor.

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 8

Satish_Rajdev
Advocate
Advocate

Have a look on this as well Click here

Best Regards,
Satish Rajdev


REY Technologies | Linked IN | YouTube Channel


 

0 Likes