How can I check if SHIFT is pressed in AUTOLISP? - autocad-plugin

In my AutoLISP code I need to check if [SHIFT] button is pressed.
In C++ I can do it by GetKeyState from user32.dll, but how to call it in AutoLISP?

(defun c:mx ()
(if (acet-sys-shift-down)
(Princ "Shift Key is Down")
(Princ "Shift Key is not Down")
)
(princ)
)

Related

How to make functions respect buffer local variables

In the (amateurish, convoluted) code below, I am trying create an environment where there may be multiple serial-term buffers/windows at the same time. I am doing everything I can think of (and just random desperate things) to make the variables local to the buffer running the terminal to which they pertain. For instance, there may be a buffer "serial-1a2b-buffer" with the terminal "serial-1a2b-term" running in it while at the same time there's a buffer "serial-3c4d-buffer" with the terminal "serial-3crd-term" running in it.
I can get the buffers/terminals setup and running with defun setupserial, but defun killserial and defun resetserial don't get the right value for "termname" and "buffname". It might be the values in the other buffer or it may be past values for buffers and terminals that no longer exist.
(In case anyone is wondering, I do a lot of work with microcontrollers. If the serial connection to them is interrupted, like with a hardware reset, then the serial process dies. The idea was to have a quick way to reset the connection - like with a function bound to a key sequence.)
(defvar serialspeed "115200")
(defvar serialport "/dev/ttyACM0")
(defvar serialbasename "serial")
(require 'term)
(defun setupserial (serialport serialspeed)
(interactive
(list
(read-string
(format "Serial Port (%s): "
serialport)
nil nil
serialport)
(read-string
(format "Speed (%s): "
serialspeed)
nil nil
serialspeed)))
(setq uniqueid (format "%04x" (random (expt 16 4))))
(setq serialid (concat serialbasename "-" uniqueid))
(setq buffname (concat serialid "-buffer"))
(setq termname (concat serialid "-term"))
(setq bufferid (get-buffer-create buffname))
(setq procid (make-serial-process
:speed (string-to-number serialspeed)
:port serialport
:name termname
:buffer buffname))
(switch-to-buffer bufferid)
(make-local-variable 'serialid)
(make-local-variable 'buffname)
(make-local-variable 'bufferid)
(make-local-variable 'termname)
(make-local-variable 'procid)
(make-local-variable 'serialspeed)
(make-local-variable 'serialport)
(term-mode)
(term-char-mode)
(local-set-key (kbd "M-r") #'resetserial)
(local-set-key (kbd "M-k") #'killserial)
(local-set-key (kbd "M-x") #'execute-extended-command)
(local-set-key (kbd "M-o") #'ace-window)
(message "Started Serial Terminal"))
(defun resetserial ()
(interactive)
(make-serial-process
:speed (string-to-number serialspeed)
:port serialport
:name termname
:buffer bufferid)
(message "Restarted Serial Terminal"))
(defun killserial ()
(interactive)
(delete-process termname))
(global-set-key (kbd "C-c s") #'setupserial)
(provide 'setup-serial)
Your problems are sequential. Having created all of your buffer-local variables, you are then destroying them all by calling a new major mode.
The section on "Derived modes, and mode hooks" in this answer might be useful reading, but the key point is that the first thing that happens when you call a major mode is kill-all-local-variables.
Because you are setting global values too, in the absence of local values your other commands will end up using whatever the most-recent global value happened to be.
Set the major mode first.

Export Selected Text From AutoCad

I am wondering if any of you fine people can point me in the right direction. I came up with an ssget function that selects the text I'm looking for but I'm not quite sure where to go from there to extract the text to either a txt or csv file.
Here is the ssget function that is working for me
(ssget "_X" '((0 . "TEXT,MTEXT")(1 . "ETCH*,MARK*,STAMP*")))
I need to grab this text from a folder full of drawings and export it to preferably a csv file where I can easily read.
Thanks in advance!
Allan
As per your question, you are extracting text from all drawings of the respective folder you can use ObjectODBX method here you can run this code directly and it extract text from drawing and create csv file with drawing name in the same folder try it you can add filter condition if you required.
(Defun C:ExtractFolderToCSV( / dwgfile filelist textstring f doc LM:GetDocumentObject FolderBox folderpath)
;; Get Document Object - Lee Mac
;; Retrieves the VLA Document Object for the supplied filename.
;; The Document Object may be present in the Documents collection, or obtained through ObjectDBX.
;; It is the callers responsibility to release such object.
;;This function I collect from Lee-Mac Thanks lee
(defun LM:GetDocumentObject (dwg / app dbx dwl err vrs)
(cond
((not (setq dwg (findfile dwg))) nil)
((cdr
(assoc
(strcase dwg)
(vlax-for doc
(vla-get-documents (setq app (vlax-get-acad-object)))
(setq dwl
(cons (cons (strcase (vla-get-fullname doc)) doc) dwl)
)
)
)
)
)
((progn
(setq dbx
(vl-catch-all-apply
'vla-getinterfaceobject
(list app
(if (< (setq vrs (atoi (getvar 'acadver))) 16)
"objectdbx.axdbdocument"
(strcat "objectdbx.axdbdocument." (itoa vrs))
)
)
)
)
(or (null dbx) (vl-catch-all-error-p dbx))
)
(prompt "\nUnable to interface with ObjectDBX.")
)
((vl-catch-all-error-p
(setq err (vl-catch-all-apply 'vla-open (list dbx dwg)))
)
(prompt (strcat "\n" (vl-catch-all-error-message err)))
)
(dbx)
)
)
;This function for select folder
(defun FolderBox (message directory flag / folder sh)
;;I found thiscode on web I am not remember website. sorry for that
;; Arguments:
;; message: the message displayed in th dialog box
;; directory: the directory to browse
;; flag values:
;; 0 = Default
;; 1 = Only file system folders can be selected. If this bit is set, the OK button is disabled if the user selects a folder that doesn't belong to the file system (such as the Control Panel folder).
;; 2 = The user is prohibited from browsing below the domain within a network (during a computer search).
;; 4 = Room for status text is provided under the text box.
;; 8 = Returns file system ancestors only.
;; 16 = Shows an edit box in the dialog box for the user to type the name of an item.
;; 32 = Validate the name typed in the edit box.
;; 512 = None "New folder" button
;; 4096 = Enables the user to browse the network branch of the shell's namespace for computer names.
;; 8192 = Enables the user to browse the network branch of the shell's namespace for printer names.
;; 16384 = Allows browsing for everything.
(vl-load-com)
(setq shell (vlax-create-object "Shell.Application"))
(if (setq
folder (vlax-invoke shell 'browseforfolder 0 message flag directory)
)
(setq folder (vlax-get-property (vlax-get-property folder 'self) 'path))
(setq folder nil)
)
(vlax-release-object shell)
folder
)
(setq folderpath (FolderBox "Select Folder" "D:/" 0))
(if (setq filelist (vl-directory-files (strcat folderpath "/") "*.dwg" 1))
(foreach dwgfile filelist
(setq
f (open
(strcat folderpath
"/"
(vl-string-subst ".CSV" ".DWG" (strcase dwgfile))
)
"w"
)
) ;create csv file in same folder with replaceing .dwg to .csv
(if (setq doc (LM:GetDocumentObject (strcat folderpath "/" dwgfile)))
(progn
(vlax-for lyt (vla-get-layouts doc)
(vlax-for obj (vla-get-block lyt)
(if
(or
(= "AcDbMText" (vla-get-objectname obj));select onlly m_text and text
(= "AcDbText" (vla-get-objectname obj))
)
(progn
(setq textstring
(vla-get-TextString obj)
)
(if
(or (= (vl-string-search "ETCH" textstring) 0) ;your test condition
(= (vl-string-search "MARK" textstring) 0)
(= (vl-string-search "STAMP" textstring) 0)
)
(write-line textstring f)
)
)
)
)
)
(vlax-release-object doc)
)
)
(close f)
)
)
);close defun
Hope this helps

ELISP: How to trap display-completion-list to a variable

I have a list of targets and I want to write a function where you can choose the
current target. My code looks like that below.
The problem is that when I do "M-x my-test", current_target is set as nil and the chosen
address is printed on the current buffer.
How do I trap the buffer output to current_target? Or my whole approach is wrong?
Please advice? Which doc to read?
Thanx
-Siddhartha
(defvar target-list '( ("10.25.110.113" " -> target-1")
("10.25.110.114" " -> target-2")) "List of Target boxes")
(defvar current-target "0.0.0.0" "Current target")
(defun my-test ()
(interactive)
(with-output-to-temp-buffer "*Target List*"
(princ "\nPlease click on IP address to choose the target\n\n")
(setq current-target (display-completion-list target-list))))
Not sure exactly what behavior you want. But if you just want to let a user choose one of your strings, then try using completing-read:
(defun my-test ()
(interactive)
(setq current-target (completing-read "Target: " target-list nil t)))
Or if you want to return the associated target then look up the string chosen in your alist:
(defun my-test ()
(interactive)
(let (target)
(setq current-target (completing-read "Target: " target-list nil t)
target (cdr (assoc current-target target-list)))
(message "Target: %s" target)))
You get the idea.
;; The code for the question after the reply from Drew is as follows
;; The idea is to present to the user names to choose from.
;; Thanx Drew for "giving the idea"
(defvar target-assoc-list '( ("Fire" . "10.25.110.113") ("Earth" . "10.25.110.114")
("Water" . "10.25.110.115") ("Air" . "10.25.110.116"))
"The assoc list of (name . ip-addr) so that user chooses by name
and current-target is assigned the ip address")
(defvar current-target "0.0.0.0")
(defun my-select-target ()
(interactive)
(let (name)
(setq name (completing-read "Enter Target (TAB for list): "
target-assoc-list nil t)
current-target (cdr (assoc name target-assoc-list)))
(message "Chosen current-target IP address: %s name: %s" current-target name)))

Prevent Emacs from modifying the OS X clipboard?

How can I prevent Emacs from ever modifying the OS X clipboard unless I explicitly ask it to?
I've tried all of:
(setq x-select-enable-clipboard nil)
(setq interprogram-cut-function nil)
(setq x-select-enable-primary nil)
(setq mouse-drag-copy-region nil)
Which does prevent kill/yank from modifying the clipboard, but selected text is still put on the clipboard.
This is GNU Emacs.app on OS X.
What else should I try?
After doing some digging into the same issue, I believe that the issue actually lies in the Emacs x-select-text function, which explicitly ignores the value of x-select-enable-clipboard on NextStep (and OS X is a NextStep).
I've "solved" this problem by replacing x-select-text with a no-op function, then explicitly using ns-{get,set}pasteboard for interprogram{cut,paste}-function:
; Override the default x-select-text function because it doesn't
; respect x-select-enable-clipboard on OS X.
(defun x-select-text (text))
(setq x-select-enable-clipboard nil)
(setq x-select-enable-primary nil)
(setq mouse-drag-copy-region nil)
(setq interprogram-cut-function 'ns-set-pasteboard)
(setq interprogram-paste-function 'ns-get-pasteboard)
Here is the original x-select-text code:
(defun x-select-text (text)
"Select TEXT, a string, according to the window system.
On X, if `x-select-enable-clipboard' is non-nil, copy TEXT to the
clipboard. If `x-select-enable-primary' is non-nil, put TEXT in
the primary selection.
On MS-Windows, make TEXT the current selection. If
`x-select-enable-clipboard' is non-nil, copy the text to the
clipboard as well.
On Nextstep, put TEXT in the pasteboard (`x-select-enable-clipboard'
is not used)."
(cond ((eq (framep (selected-frame)) 'w32)
(if x-select-enable-clipboard
(w32-set-clipboard-data text))
(setq x-last-selected-text text))
((featurep 'ns) ; This is OS X
;; Don't send the pasteboard too much text.
;; It becomes slow, and if really big it causes errors.
(ns-set-pasteboard text)
(setq ns-last-selected-text text))
(t
;; With multi-tty, this function may be called from a tty frame.
(when (eq (framep (selected-frame)) 'x)
(when x-select-enable-primary
(x-set-selection 'PRIMARY text)
(setq x-last-selected-text-primary text))
(when x-select-enable-clipboard
(x-set-selection 'CLIPBOARD text)
(setq x-last-selected-text-clipboard text))))))

How to wait for / capture aysnchronous shell command output in emacs lisp?

If I execute a shell command asynchronously in emacs lisp like so:
(shell-command "mycommand &")
Is there a way to wait for the command to generate output before continuing? For my current application, it is probably sufficient to wait until the command generates any output at all, but ideally I'd like to capture the output for additional processing. Is this possible?
You should use comint-output-filter-functions variable that contains function to call after output is inserted into the buffer.
For example, you can do :
(add-hook 'comint-output-filter-functions '(lambda (txt) (message "hello")))
N.B. : From Emacs 23.2, you have the new command async-shell-command, bound globally to M-&.
This executes your command asynchronously without requiring an ampersand. The output of your command is sent to the buffer
*Async Shell Command*.
Perhaps you need to register a Process Filter to give you the callback timing you need? See 37.9 Receiving Output from Processes in the Elisp manual (I see this in my copy for Emacs 22.3).
Here is an example of running a callback when you get the first process output and also storing it into an "associated buffer". Copy it to your *scratch* buffer and eval-region it, but make sure to split-window and show the *Messages* buffer visible so that you can see what's going on.
;; this is emacs lisp (and a comment line)
(defvar my-callback-got-some-already nil)
(defun my-callback ()
(message "callback ran at %s" (current-time-string)))
(defun my-filter-waits-for-first-time-input (proc string)
(unless my-callback-got-some-already
(setq my-callback-got-some-already t)
;; do your one-time thing
(my-callback))
;; insert into the associated buffer as if no process filter was
;; registered
(with-current-buffer (process-buffer proc)
(let ((moving (= (point) (process-mark proc))))
(save-excursion
;; Insert the text, advancing the process marker.
(goto-char (process-mark proc))
(insert string)
(set-marker (process-mark proc) (point)))
(if moving (goto-char (process-mark proc))))))
(defun async-callback-test-harness ()
(interactive)
(let ((process-handle "async-callback-test")
(associated-process-buffer "*async-callback-test*")
(command "ls")
(busy-loop-var ""))
(setq my-callback-got-some-already nil)
(message "start test %s" (current-time-string))
(start-process process-handle associated-process-buffer command)
;; Supposedly async but Emacs doesn't get the input until
;; "Emacs is waiting" so the following set-process-filter
;; can be registered in time.
;; To prove the point, make emacs busy loop to show that the
;; emacs doesn't drop its input and
;; the callback will get the unskipped input.
(switch-to-buffer associated-process-buffer)
(dotimes (k 2000) ; about two seconds on my machine
(setq busy-loop-var (concat busy-loop-var "busy looping...")))
(message "done busy waiting %s" (current-time-string))
(set-process-filter (get-process process-handle)
'my-filter-waits-for-first-time-input)
nil))
;; run it!
(async-callback-test-harness)

Resources