Open document and connect to active document - what's difference?

Open document and connect to active document - what's difference?

hydroconstructor
Explorer Explorer
1,430 Views
7 Replies
Message 1 of 8

Open document and connect to active document - what's difference?

hydroconstructor
Explorer
Explorer

I have to process some files. I write code which opens file, process it, closes, opens next file and so on. And - this code do not work.

Code:

; Select rectangle region and delete all inside it:
(defun DelRegion ( doc rect / ssetObj modItem )
  ; Create the selection set:
  (setq ssetObj (vla-Add (vla-get-SelectionSets doc) "FRAME"))
  ; select objects to delete:
  (vla-SelectByPolygon ssetObj acSelectionSetCrossingPolygon rect)
  ; delete them.
  (vlax-for modItem ssetObj
    (vla-Delete modItem)
  ) 
  (vla-Delete ssetObj)   ; Delete the selection set.
)

; select filename and launch file proceed procedure:
(defun C:batchprocrunner ( / acadObj doc )
  (setq acadObj (vlax-get-acad-object))
  ; 1 option: open file:
  (setq doc (vla-Open (vla-get-Documents acadObj) "file.dwg"))

  ; 2 option: connect to opened file:
  (setq doc (vla-get-Activedocument acadObj))

  ; Run the function:
  (DelRegion doc FSAArray1)
)

The problem is: my code works well if I comment out 1 option and choose 2 one (manually open file, activate it and get link to active document).

If I do vice-versa (comment out 2 option and try to open document) - code do not work.

What can I do with it?

0 Likes
1,431 Views
7 Replies
Replies (7)
Message 2 of 8

devitg
Advisor
Advisor

@hydroconstructor  

 

I do not know much about it , but I did some test 

 

Option 1 give you the document as not Active 

devitg_0-1613597102540.png

so need to use 

Option 2   

As it , give it as active 

devitg_1-1613598649238.png

 

 

 

0 Likes
Message 3 of 8

john.uhden
Mentor
Mentor

@devitg 

That makes sense because he has to be in an active document in order to run AutoLisp at all.  So, on an Open, it appears that he needs the new drawing to become active to perform the balance of his routine on it.

Anyway, don't you have to turn on some LispInit (?) thingy to carry instructions from one drawing to the next?  I have never tried that.

John F. Uhden

0 Likes
Message 4 of 8

Sea-Haven
Mentor
Mentor

maybe something like this 

(defun openblk (blkname / adocs)
(setq acDocs (vla-get-documents (vlax-get-acad-object)))
(vla-open acDocs blkname)
(vla-activate (vla-item acdocs 1))
)
0 Likes
Message 5 of 8

hydroconstructor
Explorer
Explorer

Thank you to your attention! This is close to be true - but do not work. Just opened document really do not active. It's "Active" property is "vlax-false". I added activation command in my procedure, now it looks like that:

; select filename and launch file proceed procedure:
(defun C:batchprocrunner ( / acadObj doc docs item docname)
  (setq acadObj (vlax-get-acad-object))
  (setq docs (vla-get-documents acadObj))
  (setq doc (vla-Open docs "filename.dwg"))
  (vla-activate (vla-item docs 1))
  (DelRegion doc FSAArray1) ; here "filename.dwg" still not active :-(
)

And after it "Active" property still "vlax-false". How else can I activate my document?

0 Likes
Message 6 of 8

martti.halminen
Collaborator
Collaborator

I believe the problem here is that running in MDI mode (the default nowadays) each AutoCAD drawing has its own separate Lisp environment, so an AutoLISP program can't change to another drawing and continue running there.

 

Changing to SDI (Single Document Interface) is one way to do this.

 

Another is writing your program to operate on one drawing, and use some outside application to jump between drawings and start your program for each.

 

- or some tricks with acaddoc.lsp loading stuff from a temporary task queue.

0 Likes
Message 7 of 8

john.uhden
Mentor
Mentor
Yes, something like that. Of course you'll have to find out which item is
the drawing you just opened, unless you can use the dwgname as the item
identifier. I've never tried it so I dunno.

John F. Uhden

0 Likes
Message 8 of 8

Sea-Haven
Mentor
Mentor

I only opened another dwg bit like open a xref do a Copy close then paste.

 

Scripts are the way to go you can write a script in a current dwg then run it, the script can have lisp code in it. As you close the open file should return back to original dwg.

 

Aeccoreconsole is another method as it works in the background and is very fast.

0 Likes