Jump to content

returning focus to Excel after lisp ends


Tomislav

Recommended Posts

Posted (edited)
(defun ActivateExcel ()
    ;; Initialize Excel application
    (setq excelApp (vlax-get-or-create-object "Excel.Application"))

    ;; Get the active workbook name
    (setq activeWorkbook (vlax-get-property excelApp 'ActiveWorkbook))
    (setq workbookName (vlax-get-property activeWorkbook 'Name))

    ;; Combine the workbook name with the standard Excel window title suffix
    (setq excelTitle (strcat workbookName " - Excel"))

    ;; Create a WScript.Shell COM object
    (setq shell (vlax-create-object "WScript.Shell"))

    ;; Activate the Excel application by its window title
    (vlax-invoke-method shell 'AppActivate excelTitle)

    ;; Release the COM objects
    (vlax-release-object shell)
    (vlax-release-object activeWorkbook)
) ;_ _defun

;***********************************************************

(defun ActivateExcel ()
  ;; Initialize Excel application
  (setq excelApp (vlax-get-or-create-object "Excel.Application"))
  
  ;; Make sure the Excel application is visible
  (vlax-put excelApp 'Visible :vlax-true)

  ;; Get the HWND of the Excel application
  (setq hwnd (vlax-get excelApp 'HWND))
  
  ;; Use WScript.Shell to bring the Excel window to the foreground
  (setq shell (vlax-create-object "WScript.Shell"))
  (vlax-invoke-method shell 'AppActivate hwnd)
  
  ;; Release the COM objects
  (vlax-release-object shell)
  (vlax-release-object excelApp)
)

hello everyone, I'm in need of your help cause I've tried everything I found and nothing works to get focus to stay on Excel after end of lisp, after populating some cells with data, simply focus always returns to cad...here are just two of my latest attempts

Edited by Tomislav
Link to comment
Share on other sites

Posted (edited)

I had buttons in excel that would called lisp in cad when I needed to export & work in excel. Might have to add a wait time

 

Sub RunLISPCommand()
  ' Declare the AutoCAD application object
  Dim acadApp As AcadApplication
  ' Get the AutoCAD application object
  Set acadApp = ThisDrawing.Application
  ' Send the LISP command to AutoCAD
  acadApp.ActiveDocument.SendCommand "Your Command HERE" & vbCr
  ...
  AppActivate "Microsoft Excel"
End Sub

 

Maybe try in

https://www.cadtutor.net/forum/forum/57-net-objectarx-amp-vba/

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

Mhupp's idea is that you run from Excel rather than from CAD, instead of a lisp yes you would write a VBA macro. What are you trying to do when reading from Excel.

 

I have a simple draw a LIne Circle and Pline in a Excel macro. 

 

I will test what happens if you can load a lisp run it and end it reading Ecel ells. But return to Excel.

 

The simplest way may be the last line in your lisp is do this.

(alert "Please select Excel again after pressing OK")

 

Link to comment
Share on other sites

Posted (edited)

I'm not reading from excel, but writing to excel cross section data from cad...I know I can just click on excel on task bar and bring it back to focus, but I'm trying to do it automatically on the end of lisp...the function ActivateExcel I would call on the end to bring it to focus

Edited by Tomislav
Link to comment
Share on other sites

Posted (edited)

Sadly still no time to do any serious lisping, so much to do and so little time ...

Anyways , maybe one of these commands :

 

(vlax-put-property excelApp 'visible :vlax-true)
(vlax-put-property 
excelApp 'ScreenUpdating :vlax-true)
(vlax-put-property 
excelApp 'DisplayAlerts :vlax-false)

(vla-put-windowstate (vlax-get-acad-object) acMin)
(vla-put-windowstate 
excelApp acMax)

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

Posted (edited)

Thanks rlx this one sort of works for me. It only works if you have minimised the window. 

 

(vla-put-windowstate excelApp acMax)

Also running dual screens. 

 

Need a Powershell script. ie winodws OS level.

Ok found something now have to remember how to incorporate into a Powershell script and run from CAD, have done it before.

$wshell = New-Object -ComObject wscript.shell
$wshell.AppActivate('Excel')

If you want to try, go bottom right in windows type cmd then powershell paste the 2 lines. Have an Excel open.

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

12 hours ago, rlx said:

Sadly still no time to do any serious lisping, so much to do and so little time ...

Anyways , maybe one of these commands :

 

(vlax-put-property excelApp 'visible :vlax-true)
(vlax-put-property 
excelApp 'ScreenUpdating :vlax-true)
(vlax-put-property 
excelApp 'DisplayAlerts :vlax-false)

(vla-put-windowstate (vlax-get-acad-object) acMin)
(vla-put-windowstate 
excelApp acMax)

 finally, BIG thanx 🙌, trying one by one with first one (of course 😊) didn't work, but when I've put them all it stayed on excel, seems like minimizing and maximizing did the trick...and thanx to others that tried to help me for spending their time to solve my problem 👍

Link to comment
Share on other sites

As I suggested the maximizing worked if you had minimized manually did not think do that and then do maximise. I think rlx was trying to tell us something. 

Link to comment
Share on other sites

Only remembered when selecting a range in excel you have to shift focus from AutoCad to Excel (and back) so just browsed through a few of my old programs and cut & pasted these lines. But to be honest , and I think this goes for most of us , a lot of my code is 'Frankenized' together from code I googled across time and space and put a little of my own scent on it. And sometimes , after so many years , having no active memory anymore ever written it 😁

 

🐉

Link to comment
Share on other sites

Yeah just had another look yes there are duplicates backups etc but 3779 lsp files.

 

I did something for a client where we used a Excel look up table and would now add the make Excel current to code rather than an Alert.

Link to comment
Share on other sites

29 minutes ago, BIGAL said:

As I suggested the maximizing worked if you had minimized manually did not think do that and then do maximise. I think rlx was trying to tell us something. 

yeah, sometimes the solution is the simplest of thing...

 

9 minutes ago, rlx said:

Only remembered when selecting a range in excel you have to shift focus from AutoCad to Excel (and back) so just browsed through a few of my old programs and cut & pasted these lines. But to be honest , and I think this goes for most of us , a lot of my code is 'Frankenized' together from code I googled across time and space and put a little of my own scent on it. And sometimes , after so many years , having no active memory anymore ever written it 😁

 

🐉

that's the way I mostly do it cause I do have some knowledge, but for something more complicated I have to copy+tweak...

 

 

I have one quick question now that I have your 'focus' 😀...can I pass selection set to copyWithBasePoint command somehow?

Link to comment
Share on other sites

I use a few simple toolbar buttons for copy with basepoint. Check if sysvar 'pickfirst' is set to 1

 

image.png.08226e0b623dafec8a6a437d0161e6f7.png

 

First I select objects and then click on button of my choise.

 

ID_Bcopy       [_Button("Copy With Basepoint 0,0", "bcopy.bmp", "ICON_16_BLANK")]^C^C_copybase;0,0;
ID_Bpaste      [_Button("Paste with Basepoint 0,0", "bpaste.bmp", "ICON_16_BLANK")]^C^C_pasteclip;0,0;
ID_GetBcopy    [_Button("Copy With Get Basepoint", "bcopy_get.bmp", "ICON_16_BLANK")]^C^C_copybase;
ID_GetBpaste   [_Button("Paste with Get Basepoint", "bpaste_get.bmp", "ICON_16_BLANK")]^C^C_pasteclip;

 

First 2 lines always use 0,0 as base point and last 2 lines allows to enter base point manually

 

in lisp you would do something lile (command "_copybase" pause "0,0") or  (command "_copybase" pause "0,0")

Link to comment
Share on other sites

i have similar buttons also, but i'm trying to add objects in SS while i'm creating them, in a lisp, and then pass them to copybp, so i can call pastetooriginal  with created script to copy them to another drawing, cause can't copy them other way through lisp...

if i had the knowledge, easiest would be to modify Lee's  Copy2DrawingsV1-3.lsp to accept selection set instead of selecting objects, and instead of selecting file to where to copy it would create new file...but that's too much for me

Link to comment
Share on other sites

(defun c:cwb ( / ss pt) (if (and (setq pt (getpoint "\nSelect basepoint")) (setq ss (ssget))) (command "_copybase" pt ss "")))
(defun c:pwb () (command "pasteclip" pause))

 

  • Like 1
Link to comment
Share on other sites

ok, thank you, so the ss can be passed normally to copybase, I will supply pt and ss , so it is:

 

(defun c:cwb ( ss pt /) (if (and (= pt T)(= ss T)) (command "_copybase" pt ss "")))

 

Link to comment
Share on other sites

"add objects in SS while i'm creating them, in a lisp,"

 

Look into lisp function SSADD you should be able to add (entlast) to ss.

 

(setq ss (ssadd)) ; makes a blank selection set
do stuff
(setq ss (ssadd (entlast) ss))

 

  • Like 1
Link to comment
Share on other sites

12 hours ago, BIGAL said:

"add objects in SS while i'm creating them, in a lisp,"

 

Look into lisp function SSADD you should be able to add (entlast) to ss.

 

(setq ss (ssadd)) ; makes a blank selection set
do stuff
(setq ss (ssadd (entlast) ss))

 

exactly what my idea was, after every object add it to ss, and on the end after being copied, delete them...   

Link to comment
Share on other sites

You can set a "place holder"  right before creating or modifying things in the drawing. using entnext to add each to the selection set.

 

(setq ss (ssadd))  ; Create an empty selection set
(setq LastEnt (entlast))  ; Get the last entity in the drawing

; code to add items

(while (setq LastEnt (entnext LastEnt)) ; Add all entity's created/modified after setting lastent to the selection set
  (ssadd LastEnt ss)  
)

 

  • Like 1
Link to comment
Share on other sites

10 hours ago, mhupp said:

You can set a "place holder"  right before creating or modifying things in the drawing. using entnext to add each to the selection set.

 

(setq ss (ssadd))  ; Create an empty selection set
(setq LastEnt (entlast))  ; Get the last entity in the drawing

; code to add items

(while (setq LastEnt (entnext LastEnt)) ; Add all entity's created/modified after setting lastent to the selection set
  (ssadd LastEnt ss)  
)

 

that is very useful, thanx, was wondering if there is something like startundomark but for selecting objects and this is it....

it's a shame autolisp can't access other drawings directly like vba, so scripts must be made to circumvent that...they pretty much slow down things   

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