Author Topic: Need another eyes to check whats wrong with this lisp  (Read 2041 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1423
Need another eyes to check whats wrong with this lisp
« on: September 17, 2014, 09:44:15 AM »
I wrote this lisp to change Dynamic block properties using Lee subroutine.
this lisp insert a foundation block in CG of the column
I am facing 2 problems
- The changes not exact what i need when change value of variables W & L
For example change
Code: [Select]
      (setq w 6400) ; Foundation Width
      (setq l 6000) ; Foundation Length
To
Code: [Select]
      (setq w 3400) ; Foundation Width
      (setq l 3000) ; Foundation Length

- Start undo mark cases an error
Code: [Select]
(vla-StartUndoMark spc)error is
Code: [Select]
Error: ActiveX Server returned the error: unknown name: StartUndoMark
The code
Code: [Select]
(defun c:IFB ( / DIFL DIFW DOC HORDIM-01 HORDIM-02 L OBJ P1 P1X P1Y P2 P2X P2Y SPC VERDIM-01 VERDIM-02 W XDIF YDIF )
  (vl-load-com)
  (setq *acad (cond (*acad) ((vlax-get-acad-object))))
  (setq doc (vla-get-ActiveDocument (setq *acad (vlax-get-Acad-Object)))
spc (if (zerop (vla-get-activespace doc)) (if (= (vla-get-mspace doc) :vlax-true) (vla-get-modelspace doc) (vla-get-paperspace doc)) (vla-get-modelspace doc)))

  (if (vl-cmdf "_.-insert" "R:/SDC_LIBRARIES/CAD_LIBRARY/BLOCKS/S/Fx.dwg" "0,0,0" "1" "1" "0") ; inser Foundation block
    (progn
      ;(vla-StartUndoMark spc)
      (vl-cmdf "_.erase" "last" "")
      (vl-cmdf "_.-purge" "blocks" "fx" "n")
      (vl-cmdf "_.-DIMSTYLE" "Restore" "SDC_DIM")     
      (setq w 6400) ; Foundation Width
      (setq l 6000) ; Foundation Length
      (setq p1 (getpoint "\nPick Column CG"))
      (setq p2 (getpoint "\nPick Axes Crossing point"))
      (setq p1 (trans p1 1 0 ))
      (setq p2 (trans p2 1 0 ))
      (setq p1X (nth 0 p1))
      (setq p1Y (nth 1 p1))
      (setq p2X (nth 0 p2))
      (setq p2Y (nth 1 p2))
      (setq XDif (- p1X p2X))
      (setq YDif (- p1Y p2Y))
      (setq Difw (/ (- 6400 w) 2))
      (setq Difl (/ (- 6000 l) 2))
      (setq VerDim-01 (+ Difl (- (/ l 2) YDif)))
      (setq VerDim-02 (+ Difl (- l VerDim-01)))
      (setq HorDim-01 (+ Difw (- (/ w 2) XDif)))
      (setq HorDim-02 (+ Difw (- w HorDim-01)))
      (vl-catch-all-error-p (setq obj (vl-catch-all-apply
(function vla-InsertBlock)
(list spc (vlax-3D-point p1) "F00" 1.0 1.0 1.0 0))))
      (vla-put-layer obj "S-FOND-EDGE")
      (LM:setdynprops obj (list (cons "VerDim-01" VerDim-01)
(cons "VerDim-02" VerDim-02)
(cons "HorDim-01" HorDim-01)
(cons "HorDim-02" HorDim-02)
(cons "Width" w)
(cons "Length" l)
(cons "Visibility1" "WithDimentions" )))
      ;(vla-EndUndoMark spc)
      )
    (princ "\nUnable to Copy Block From Drawing.")
    ) 
  )


    (defun *error* (msg)
      (cond
((not msg))
((wcmatch (strcase msg) "*QUIT*,*CANCEL*"))
(T (princ (strcat "\nError: " msg)))
)
      (redraw)
      ;(vla-EndUndoMark doc)
      (princ)
      ) ;end error

;; Set Dynamic Block Properties  -  Lee Mac
;; Modifies values of Dynamic Block properties using a supplied association list.
;; blk - [vla] VLA Dynamic Block Reference object
;; lst - [lst] Association list of ((<Property> . <Value>) ... )
;; Returns: nil
(defun LM:setdynprops (blk lst / itm)
  (setq lst (mapcar '(lambda (x) (cons (strcase (car x)) (cdr x))) lst))
  (foreach x (vlax-invoke blk 'getdynamicblockproperties)
    (if (setq itm (assoc (strcase (vla-get-propertyname x)) lst))
      (vla-put-value
x
(vlax-make-variant
  (cdr itm)
  (vlax-variant-type (vla-get-value x))
)
      )
    )
  )
) ; END DEFUN - LM:setdynprops

Thanks  All

kpblc

  • Bull Frog
  • Posts: 396
Re: Need another eyes to check whats wrong with this lisp
« Reply #1 on: September 17, 2014, 10:08:24 AM »
Why are you use (vla-StartUndoMark spc) ? You should use (vla-StartUndoMark doc). And (i think) the code could be like this:
Code: [Select]
(vl-load-com)

(defun c:ifb (/        *error*  lm:setdynprops    difl     difw     doc      hordim-01         hordim-02
              l        obj      p1       p1x      p1y      p2       p2x      p2y      spc      verdim-01
              verdim-02         w        xdif     ydif
              )

  (defun *error* (msg)
    (cond
      ((not msg))
      ((wcmatch (strcase msg) "*QUIT*,*CANCEL*"))
      (t (princ (strcat "\nError: " msg)))
      ) ;_ end of cond
    (redraw)
    (vla-EndUndoMark doc)
    (princ)
    ) ;_ end of defun


  ;; Set Dynamic Block Properties  -  Lee Mac
  ;; Modifies values of Dynamic Block properties using a supplied association list.
  ;; blk - [vla] VLA Dynamic Block Reference object
  ;; lst - [lst] Association list of ((<Property> . <Value>) ... )
  ;; Returns: nil
  (defun lm:setdynprops (blk lst / itm)
    (setq lst (mapcar '(lambda (x) (cons (strcase (car x)) (cdr x))) lst))
    (foreach x (vlax-invoke blk 'getdynamicblockproperties)
      (if (setq itm (assoc (strcase (vla-get-propertyname x)) lst))
        (vla-put-value
          x
          (vlax-make-variant
            (cdr itm)
            (vlax-variant-type (vla-get-value x))
            ) ;_ end of vlax-make-variant
          ) ;_ end of vla-put-value
        ) ;_ end of if
      ) ;_ end of foreach
    ) ;_ end of defun

  (setq *acad (cond (*acad)
                    ((vlax-get-acad-object))
                    ) ;_ end of cond
        doc   (vla-get-activedocument *acad)
        spc   (if (zerop (vla-get-activespace doc))
                (if (= (vla-get-mspace doc) :vlax-true)
                  (vla-get-modelspace doc)
                  (vla-get-paperspace doc)
                  ) ;_ end of if
                (vla-get-modelspace doc)
                ) ;_ end of if
        ) ;_ end of setq

  (vla-startundomark doc)
  (if (vl-cmdf "_.-insert" "R:/SDC_LIBRARIES/CAD_LIBRARY/BLOCKS/S/Fx.dwg" "0,0,0" "1" "1" "0") ; inser Foundation block
    (progn
          ;(vla-StartUndoMark spc)
      (vl-cmdf "_.erase" "_last" "")
      (vl-cmdf "_.-purge" "_blocks" "fx" "_n")
      (vl-cmdf "_.-DIMSTYLE" "_Restore" "SDC_DIM")
      (setq w 6400) ; Foundation Width
      (setq l 6000) ; Foundation Length
      (setq p1 (getpoint "\nPick Column CG"))
      (setq p2 (getpoint "\nPick Axes Crossing point"))
      (setq p1 (trans p1 1 0))
      (setq p2 (trans p2 1 0))
      (setq p1x (nth 0 p1))
      (setq p1y (nth 1 p1))
      (setq p2x (nth 0 p2))
      (setq p2y (nth 1 p2))
      (setq xdif (- p1x p2x))
      (setq ydif (- p1y p2y))
      (setq difw (/ (- 6400 w) 2))
      (setq difl (/ (- 6000 l) 2))
      (setq verdim-01 (+ difl (- (/ l 2) ydif)))
      (setq verdim-02 (+ difl (- l verdim-01)))
      (setq hordim-01 (+ difw (- (/ w 2) xdif)))
      (setq hordim-02 (+ difw (- w hordim-01)))
      (vl-catch-all-error-p (setq obj (vl-catch-all-apply
                                        (function vla-insertblock)
                                        (list spc (vlax-3d-point p1) "F00" 1.0 1.0 1.0 0)
                                        ) ;_ end of vl-catch-all-apply
                                  ) ;_ end of setq
                            ) ;_ end of vl-catch-all-error-p
      (vla-put-layer obj "S-FOND-EDGE")
      (lm:setdynprops obj
                      (list (cons "VerDim-01" verdim-01)
                            (cons "VerDim-02" verdim-02)
                            (cons "HorDim-01" hordim-01)
                            (cons "HorDim-02" hordim-02)
                            (cons "Width" w)
                            (cons "Length" l)
                            (cons "Visibility1" "WithDimentions")
                            ) ;_ end of list
                      ) ;_ end of LM:setdynprops
          ;(vla-EndUndoMark spc)
      ) ;_ end of progn
    (princ "\nUnable to Copy Block From Drawing.")
    ) ;_ end of if
  (vla-endundomark doc)
  (princ)
  ) ;_ end of defun

P.S. I didn't check the code.
Sorry for my English.

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
Re: Need another eyes to check whats wrong with this lisp
« Reply #2 on: September 17, 2014, 11:06:44 AM »
If I'm not mistaken (forgive my fumbling with autolisp; it's been a few years). but shouldn't the declarations of the variables be something like:

Code - Auto/Visual Lisp: [Select]
  1. (setq acad
  2.       (cond
  3.         (acad)
  4.         ((vlax-get-acad-object)))
  5.       doc
  6.       (cond
  7.         (doc)
  8.         ((vla-get-activedocument acad)))
  9.       spc
  10.       (cond
  11.         (spc)
  12.         ((vla-get-modelspace doc))
  13.         ((vla-get-paperspace doc)))
  14.       )
  15.  

And then using what kpblc said by using (vla-startundomark doc).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Need another eyes to check whats wrong with this lisp
« Reply #3 on: September 17, 2014, 12:47:47 PM »
If I'm not mistaken (forgive my fumbling with autolisp; it's been a few years). but shouldn't the declarations of the variables be something like:

Code - Auto/Visual Lisp: [Select]
  1. (setq acad
  2.       (cond
  3.         (acad)
  4.         ((vlax-get-acad-object)))
  5.       doc
  6.       (cond
  7.         (doc)
  8.         ((vla-get-activedocument acad)))
  9.       spc
  10.       (cond
  11.         (spc)
  12.         ((vla-get-modelspace doc))
  13.         ((vla-get-paperspace doc)))
  14.       )
  15.  

And then using what kpblc said by using (vla-startundomark doc).

Good suggestion, though you would not want the 'spc' variable as global, since the user could potentially change the active space between uses of the program.

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
Re: Need another eyes to check whats wrong with this lisp
« Reply #4 on: September 17, 2014, 01:15:08 PM »
...
Good suggestion, though you would not want the 'spc' variable as global, since the user could potentially change the active space between uses of the program.

*shock* That was my first attempt at any lisp in YEARS!

`global' ...Ah, yes. You are sparking some lisp syntax memories now. No, you wouldn't. Again, correct me if I'm wrong but let me try to expand on that a little (an exercise for me).

The SPC variable is not a global variable perse (because it is localized in the parameter/localization declarations) so on first run the first part of that COND statement will return nil and COND will continue on to the next statement. So the code as I typed it would work (because of this) but is confusing because it is a contradiction. Therefore the correct code would be:

Code - Auto/Visual Lisp: [Select]
  1. (setq acad
  2.       (cond
  3.         (acad)
  4.         ((vlax-get-acad-object)))
  5.       doc
  6.       (cond
  7.         (doc)
  8.         ((vla-get-activedocument acad)))
  9.       spc
  10.       (cond
  11.         ((vla-get-modelspace doc))
  12.         ((vla-get-paperspace doc)))
  13.       )

How did I do?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org