Jump to content

Recommended Posts

Posted

Hello Everyone. This is my first post here. I am busy learning AutoLISP in last couple of days. I came to certain level using all the information i get from the internet.

 

When i just make a few lines of code with few local variables and start-endundo mark it is working as expected.

After executing the second code, it works as expected but when i want to undo using ctrl+z, it takes all the commands done by LISP one by one. I would like to have it so that when i press ctrl+z it just takes all the actions back (expect the saving, i mainly need to take mirror align and osnap actions back to default.). I tried to remove *error* nil and replace it with vla-endundomark doc but without success.

 

 

 

Apart from this i would also want to know how does this symbols used exactly in AutoLISP environment  % * @

 

Best Regards Kadir

 

 

Example code is here

(defun c:layson ( / LayLst)
  (setq LayLst (list "0" "Defpoints" "Dim" "Text" "Embeds"))
  (vlax-for % (cd:ACX_Layers)
    (if (member (vla-get-name %) LayLst)
      (vla-put-layeron % :vlax-true)
    )
  )
  (princ)
)
(defun cd:ACX_ADoc ()
  (or
    *cd-ActiveDocument*
    (setq *cd-ActiveDocument*
      (vla-get-ActiveDocument (vlax-get-acad-object))
    )
  )
  *cd-ActiveDocument*
)
(defun cd:ACX_Layers ()
  (or
    *cd-Layers*
    (setq *cd-Layers* (vla-get-Layers (cd:ACX_ADoc)))
  )
  *cd-Layers*
)

 

 

 

 

(defun c:MDXF ( / *error* global:ans
               ans
               mlist
               allelayers
               layerName
               dxf_name
               folder_MDXF
               msg
               osm
               ss
               sscenter
               ssctop
               centerpt
               pt1
               pt2
               c
               dist
               doc
               text-entity
               text-content
               num-entities
               num-text
               i
               entity
               entity-type
               totalsp
               current_name
               name-list)
              (vl-load-com)
(setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))  
(vla-startundomark doc)  
(defun *error* ( msg )
  (setvar "filedia" 1)
        (if osm (setvar 'osmode osm));OSnap mode other than nil, change it back to osm
        (if (not (member msg '("Function cancelled" "quit / exit abort" "*QUIT*,*CANCEL*")))
            (princ (strcat "\nError: " msg))
        )
  (princ)
  (vla-endundomark doc)
  (exit)
)
(setq name-list '())
(if (null global:ans)
(setq global:ans "Ja"))
(setq totalsp 0)
(setq folder_MDXF "C:\\Users\\kadir.oezen\\Desktop\\AutoLISP\\MaschineDXF\\") ; Set the R12 format destination folder path
(setq mlist '("CNC"
"SEQ"
"CUT"
"DRILL"
"POLISH"
"WJ"
"Ausschnitt"
"Bemassung Maschine"
"Nachschneiden"
"Granit"
))  ; set CNC Maschine Readable Layer names and keywords,
(setq osm (getvar 'osmode));osm mode saved
  
    (if (vl-file-directory-p folder_MDXF);if folder presents go on
        (progn
          (vlax-for allelayers (vla-get-layers doc)
            (vla-put-layeron allelayers :vlax-false);vlax command takes all layers and turns them off one by one
            (setq layerName (vla-get-name allelayers));set layername to the all layers
            ; Check if any of the keywords are present in the layer name
            (foreach keyword mlist
            (if (vl-string-search keyword layerName)
            (vla-put-layeron allelayers :vlax-true) ; if the name is on the list Turn on the layer there is no else command.
            )
            )
			    );--vlaxfor alle layers ende
          (while (= global:ans "Ja")
            (setq ss (ssget));ask user to make selection
            (if ss ;if ss is valid go with this
              (progn
                (setq num-entities (sslength ss))
                (setq i 0)
                (setq num-text 0)
                (while (< i num-entities)
                  (setq entity (ssname ss i))
                  (setq entity-type (cdr (assoc 0 (entget entity))))
                    (cond ((= entity-type "TEXT") (setq num-text (+ num-text 1)) (setq text-entity entity))
                    )
                  (setq i (+ i 1))
                );--while end
                (if (= num-text 0)
                  (progn
                  (alert "Es gibt kein Text Elemente in Auswähl. Programm gestoppt.")
                  (*error* "Kein Text")
                  );if numtext 0 else
                    (progn
                      (if (= num-text 1)
                        (progn  
                        (setq text-content (cdr (assoc 1 (entget text-entity))))
                        ;(alert (strcat "Text content: " text-content))
                        (setq dxf_name (strcat (substr text-content 1 5) (substr text-content 7 3) (substr text-content 11 3)));dxf_name from textcontent extract 45000001A01 zB.
                        ;(alert (strcat "DXF NAME für Maschine; " dxf_name))      
                        );if num text else
                          (progn
                          (alert "Es gibt mehr als 1 Text Elemente in Auswähl. Programm gestoppt.")
                          (*error* "mehr als 1 Text")
                          )
                      );--if num text 1 end
                    )      
                );--if numtext 0 ende
              );--if ss not validelse
              (progn 
                (alert " Kein Objekt ist ausgewählt.") 
                (*error* "kein Objekt")
              )
            ); --if ss ended
              (if (or (not(findfile (strcat folder_MDXF dxf_name ".dxf"))) (vl-file-delete (strcat folder_MDXF dxf_name ".dxf"))); if no old file found or it is deleted go on
                (progn
                  
                    (setq pt1 (getpoint "n/ Links"))
                    (setq pt2 (getpoint "n/ Rechts"))
                    (setq dist (distance pt1 pt2))
                    (setq c (getvar "viewctr"))
                    (setq sscenter (polar c 0 (/ dist 2)))
                    (setq ssctop (polar sscenter (/ pi 2) 100))
                    (setq centerpt (polar c 0 dist))
                    (setvar 'osmode 0)
                    (command-s "_.align" ss "" pt1 c pt2 centerpt "" "n")
                    (command-s "_.mirror" ss "" sscenter ssctop "_y")
                    (setvar 'osmode osm)
                    (command "SICHALS" "d" "v" "2000" "o" ss "" "16" (strcat folder_MDXF dxf_name ".dxf"))
                    (setq current_name dxf_name)
                    (setq name-list (cons current_name name-list))
                    (setq totalsp (+ totalsp 1));total dxf count
                    (initget "Ja Nein")
                      (if (setq ans (cond ((getkword (strcat "\nHaben Sie mehr Daten zu speichern? [Ja/Nein] <" global:ans ">"))) (global:ans)));cond testes if first getkword has value, if not goes to second option which is global ans
                        (progn
                        (setq global:ans ans)
                        )
                          (progn
                          )
                      )
                  ;--while globalans end
                  
                )
                (progn ;if else not findfile or delete
                  (alert "Programm gestoppt, Datei kann nicht überschrieben werden. Schließen Sie die geöffneten Dateien und versuchen Sie es erneut")
                  (*error* "Data kann man nicht überschreiben.")
                )
              );--end if or not findfile file delete
            
         );if and folder else
    );--while global ans ja end
        (progn
        (alert "Sie haben keinen Zugriff auf den Server. Stellen Sie sicher, dass Sie die Dateien auf T: lesen können.")
        (*error* "kein zugriff laufwerk T:")
        )
    );-- if end both folders
  (if (= totalsp 1)
    (progn    
    (alert (strcat (apply 'strcat (append (list (car name-list))(mapcar '(lambda (x) (strcat "\n" x)) (cdr name-list)))) " Die Datei ist im MaschineDXF Ordner gespeichert. AutoCAD Zeichnung wird jetzt ohne Speichern geschlossen."))
    )
    (progn
      (alert (strcat (apply 'strcat (append (list (car name-list))(mapcar '(lambda (x) (strcat "\n" x)) (cdr name-list)))) "\nDateien sind im MaschineDXF Ordner gespeichert. AutoCAD Zeichnung wird jetzt ohne Speichern geschlossen."))
    )
  )
  ;(command "._close" "_y")
  (*error* nil)
);--defun end

 

Posted

Here is an example code that i write and it works as expected. When I do CTRL Z it removes all of the 4 points at the same time and not one by one.

 

(defun c:zBeispiel ( / *error* c pt1 pt2 pt3 msg)
 (vl-load-com)
(setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))  
(vla-startundomark doc)  
(defun *error* ( msg )
  (and msg (not (wcmatch (strcase msg) "*CANCEL*,*QUIT*,*BREAK*"))
  (princ (strcat "\nError: " msg))
  )
(princ)
(vla-endundomark doc)
)

(setq c (getvar "viewctr"))
(setq pt1 (polar c 0 100))
(setq pt2 (polar c (/ pi 2) 100))
(setq pt3 (polar c pi 100)) 
(command "_.point" c)
(command "_.point" pt1)
(command "_.point" pt2)
(command "_.point" pt3)
(vla-endundomark doc)
(princ)
)

 

Posted

In your second code you will need a (vla-endundomark doc) at the end of the code. You have one in the error defun but if the code works as expected there is no end undo marker set.

 

 

 

I'll often write things in pairs so I don't forget, so if I add a (vla-startundomark doc), I'll then add my (vla-endundomark doc), leaving a gap for the rest of the code

Posted (edited)
5 minutes ago, Steven P said:

In your second code you will need a (vla-endundomark doc) at the end of the code. You have one in the error defun but if the code works as expected there is no end undo marker set.

 

 

 

I'll often write things in pairs so I don't forget, so if I add a (vla-startundomark doc), I'll then add my (vla-endundomark doc), leaving a gap for the rest of the code

 

 

 

Thank you very much for speedy reply. As i mentioned in the first post i also tried to add vla-endundomark doc at the end but the result is similar.
The thing i do not understand is in cmd i can see that it actually tried to take group actions back but it takes the mirror action back first than align than osnap.

 

Haben Sie mehr Daten zu speichern? [Ja/Nein] <Ja>N
Befehl: _u (LISP-Ausdruck) GRUPPE
Befehl: _u (LISP-Ausdruck) GRUPPE
Befehl: _u GRUPPE
Befehl: _u
Befehl: _u GRUPPE



Also i would like to ask one more thing. if i execute *error* nil at the end of my code, does not it also execute the vla endundo command in the error command with nil as argument. That means if everything works accordingly it should have endundomark because of the *error* function. Or am i making a logical mistake?

Edited by Kadirozen

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