Jump to content

Lisp & Dynamic Block


REID7800

Recommended Posts

I've looked for a code like this forever. Using lisp I'm looking to Insert a dynamic block, automatically select the stretch grip of the DB & When the lisp is done the DB will explode.

Link to comment
Share on other sites

  • Replies 61
  • Created
  • Last Reply

Top Posters In This Topic

  • alanjt

    22

  • Lt Dan's legs

    17

  • Lee Mac

    12

  • REID7800

    8

Top Posters In This Topic

Posted Images

I know how to insert but I do not know how to select the dynamic grip after it has been inserted. That's all I'm really looking for

Link to comment
Share on other sites

Not sure how to do this or if it can be done, but is using the dynamic block necessary? You could just write a lisp routine to draw what it is you want. If you post the block, maybe I will be clearer in what you are trying to achieve.

I have written lisp routines to draw floor joists from one to point to another, and able to enter the height. Happy to help if you post block.

Link to comment
Share on other sites

I'm not quite sure what you are trying to achieve using the LISP to select the grip - why not just select the grip manually?

 

If you wanted to modify the properties you would need to look into the code for getting at Dynamic Block properties, I have written a plethora of code for such here:

 

;; Retrieves All Properties from a Dynamic Block
;; Args: obj ~ Dynamic Block VLA-Object
;; Returns: ((<property name> <value>) ...)

(defun GetDynProps (obj)
 ;; Lee Mac  ~  07.04.10
 (mapcar
   (function
     (lambda (x / v)
       (list (vla-get-PropertyName x)
             (if (= 8192 (logand 8192 (vlax-variant-type (setq v (vla-get-value x)))))
               (vlax-safearray->list (vlax-variant-value v))
               (vlax-variant-value v)))))
     
   (vlax-invoke obj 'GetDynamicBlockProperties)))

;; Retrieves All Properties from a Dynamic Block
;; Args: obj ~ Dynamic Block VLA-Object
;; Returns: ((<property name> <value>) ...)

(defun GetDynProps (obj)
 ;; Lee Mac  ~  07.04.10
 (mapcar
   (function
     (lambda (x) (list (vla-get-PropertyName x) (vlax-get x 'Value))))

   (vlax-invoke obj 'GetDynamicBlockProperties)))

;; Changes a Dynamic Block Property Value
;; Args: obj  ~ Dynamic Block VLA-Object
;;       prop ~ Property Name
;;       val  ~ New Value
;; Returns: New Value (val)

(defun PutDynPropValue (obj prop val)
 ;; Lee Mac  ~  07.04.10
 (mapcar
   (function
     (lambda (x)
       (if (eq (strcase prop) (strcase (vla-get-propertyName x)))
         (vla-put-value x
           (vlax-make-variant val
             (vlax-variant-type (vla-get-value x)))))))

   (vlax-invoke obj 'GetDynamicBlockProperties)) val)

;; Retrieves the Allowed Values for a Specific Property
;; Args: obj  ~ Dynamic Block VLA-Object
;;       prop ~ Property Name
;; Returns: List of Allowed Values (or nil)

(defun GetPropAllowedValues (obj prop / a)
 ;; Lee Mac  ~  07.04.10
 (mapcar
   (function
     (lambda (x / w)
       (if (eq (strcase prop) (strcase (vla-get-propertyName x)))
         (setq a
           (mapcar
             (function
               (lambda (v)
                 (if (= 8192 (logand 8192 (vlax-variant-type v)))
                   (vlax-safearray->list (vlax-variant-value v))
                     (vlax-variant-value v))))
             
             (if (< 0 (vlax-safearray-get-u-bound
                        (setq w (vlax-variant-value
                                  (vla-get-AllowedValues x))) 1))
               
               (vlax-safearray->list w)))))))
   
   (vlax-invoke obj 'GetDynamicBlockProperties)) a)

;; Retrieves the Allowed Values for a Specific Property
;; Args: obj  ~ Dynamic Block VLA-Object
;;       prop ~ Property Name
;; Returns: List of Allowed Values (or nil)

(defun GetPropAllowedValues (obj prop / a)
 ;; Lee Mac  ~  07.04.10
 (mapcar
   (function
     (lambda (x)
       (if (eq (strcase prop) (strcase (vla-get-propertyName x)))
         (setq a (vlax-get x 'AllowedValues)))))

   (vlax-invoke obj 'GetDynamicBlockProperties)) a)

;; Retrieves the Allowed Values for all Properties
;; Args: obj  ~ Dynamic Block VLA-Object
;; Returns: ((<property name> (<Allowed Values>)) ...)

(defun GetAllowedValues (obj)
 ;; Lee Mac  ~  07.04.10
 (mapcar
   (function
     (lambda (x / w)
       (list (vla-get-propertyname x)
             (mapcar
               (function
                 (lambda (v)
                   (if (= 8192 (logand 8192 (vlax-variant-type v)))
                     (vlax-safearray->list (vlax-variant-value v))
                       (vlax-variant-value v))))

               (if (< 0 (vlax-safearray-get-u-bound
                          (setq w (vlax-variant-value
                                    (vla-get-AllowedValues x))) 1))
                 
                 (vlax-safearray->list w))))))
   
   (vlax-invoke obj 'GetDynamicBlockProperties)))

;; Retrieves the Allowed Values for all Properties
;; Args: obj  ~ Dynamic Block VLA-Object
;; Returns: ((<property name> (<Allowed Values>)) ...)

(defun GetAllowedValues (obj)
 ;; Lee Mac  ~  07.04.10
 (mapcar
   (function
     (lambda (x)
       (list (vla-get-propertyname x)
             (vlax-get x 'AllowedValues))))

   (vlax-invoke obj 'GetDynamicBlockProperties)))

Link to comment
Share on other sites

I'm trying to array db's. I work with people that only know basic Autocad commands. I'm trying to get something visual so that's why I went with the Dynamic block of a Dynamic block. This way they can see how far the block is going. When they're done stetching the block, just explode it and everything is back to normal.

SAMPLE.dwg

Link to comment
Share on other sites

This might give you some food for thought:

 

(defun c:InsertBlock ( / block space point )
 (vl-load-com)
 ;; Lee Mac  ~  05.05.10

 (setq block nil) ;; Block Name or nil

 (setq space
   (if
     (or
       (eq AcModelSpace
         (vla-get-ActiveSpace
           (setq doc
             (vla-get-ActiveDocument
               (vlax-get-acad-object)
             )
           )
         )
       )
       (eq :vlax-true
         (vla-get-MSpace doc)
       )
     )
     (vla-get-ModelSpace doc)
     (vla-get-PaperSpace doc)
   )
 )        

 (if (setq block (GetBlock block))
   (while
     (setq point
       (getpoint "\nSpecify Point for Insertion: "))
     
     (InsertBlock space block point)
   )
 )
 
 (princ)
)

(defun GetBlock ( block )
 ;; Lee Mac  ~  05.05.10
 (cond
   (
     (not
       (and
         (or block
           (setq block
             (getfiled "Select Block" "" "dwg" 16)
           )
         )
         (or
           (and
             (eq "" (vl-filename-extension block))
             (or
               (tblsearch "BLOCK" block)
               (setq block
                 (findfile
                   (strcat block ".dwg")
                 )
               )
             )
           )
           (setq block (findfile block))
         )
       )
     )
    nil
   )
   ( block )
 )
)

(defun InsertBlock ( Block Name Point )
 (vla-InsertBlock Block
   (vlax-3D-point Point) Name 1. 1. 1. 0.
 )
)

 

I don't know the name of the tag you are trying to populate hence I cannot proceed any further with this code.

Link to comment
Share on other sites

I wanted to array the block with lisp. That code above can get tedious when you have a long span. The Dynamic block is in the dwg above. Block name: panel

Link to comment
Share on other sites

An updated code to the above - it could be worked into an array:

 

(defun c:InsertBlock ( / block tag space point )
 (vl-load-com)
 ;; Lee Mac  ~  05.05.10

 (setq block "PANEL") ;; Block Name or nil

 (setq tag nil)   ;; Tag Name or nil

 (setq space
   (if
     (or
       (eq AcModelSpace
         (vla-get-ActiveSpace
           (setq doc
             (vla-get-ActiveDocument
               (vlax-get-acad-object)
             )
           )
         )
       )
       (eq :vlax-true
         (vla-get-MSpace doc)
       )
     )
     (vla-get-ModelSpace doc)
     (vla-get-PaperSpace doc)
   )
 )        

 (if
   (and
     (setq block (GetBlock block))
     (setq *num*
       (1-
         (cond
           (
             (getint
               (strcat "\nSpecify Starting Number <"
                 (itoa
                   (setq *num*
                     (cond ( *num* ) ( 1 ))
                   )
                 )
                 "> : "
               )
             )
           )
           ( *num* )
         )
       )
     )
   )

   (while
     (setq *num* (1+ *num*)
           point (getpoint "\nSpecify Point for Insertion: "))
     
     (if (and (setq obj (InsertBlock space block point)) tag)
       (PutAttValue obj tag (itoa *num*))
     )
   )
 )
 
 (princ)
)

(defun PutAttValue ( object tag value )
 ;; Lee Mac  ~  05.05.10
 (mapcar
   (function
     (lambda ( attrib )
       (and
         (eq tag (vla-get-TagString attrib))
         (vla-put-TextString attrib value)
       )
     )
   )
   (vlax-invoke object 'GetAttributes)
 )
 value
)

(defun GetBlock ( block )
 ;; Lee Mac  ~  05.05.10
 (cond
   (
     (not
       (and
         (or block
           (setq block
             (getfiled "Select Block" "" "dwg" 16)
           )
         )
         (or
           (and
             (vl-position
               (vl-filename-extension block) '("" nil)
             )
             (or
               (tblsearch "BLOCK" block)
               (setq block
                 (findfile
                   (strcat block ".dwg")
                 )
               )
             )
           )
           (setq block (findfile block))
         )
       )
     )
    nil
   )
   ( block )
 )
)

(defun InsertBlock ( Block Name Point )
 (if
   (not
     (vl-catch-all-error-p
       (setq result
         (vl-catch-all-apply (function vla-insertblock)
           (list Block (vlax-3D-point point) Name 1. 1. 1. 0.)
         )
       )
     )
   )
   result
 )
)

Link to comment
Share on other sites

Not exactly what I was looking for... why is it counting?

 

Dude - I'm going by what you posted previously - how to write the code better, which included a count.

Link to comment
Share on other sites

:unsure: I was getting some strange results (double blocks) using vla-explode, so if you get the same thing, remove it and uncomment the vl-cmdf expolde marked in red.

 

REMOVED

Link to comment
Share on other sites

Bear in mind Alan that vla-explode will leave the original block in the drawing and the new objects will be copies. :)

Ahh, OK. That makes sense.

 

Try this instead...

(defun c:LongWall (/ blockname p1 p2)
 ;; Insert and array "LONG WALL" block (required)
 ;; Alan J. Thompson, 05.05.10
 (vl-load-com)

 (setq blockname "LONG WALL")

 (if (and (or (tblsearch "block" blockname)
              (findfile (strcat blockname ".dwg"))
              (alert (strcat blockname " cannot be found!"))
          )
          (setq p1 (getpoint "\nSpecify block insertion point: "))
          (setq p2 (getpoint p1 "\nSpecify block end point: "))
     )
   ((lambda (block)
      (foreach x (vlax-invoke block 'GetDynamicBlockProperties)
        (and (eq (vla-get-propertyname x) "Distance")
             (vla-put-value
               x
               ((lambda (dist / num)
                  (cond
                    ((>= 1. (/ dist 46.)) 46.)
                    ((< 1. (setq num (/ dist 46.))) (* 46. (1+ (fix num))))
                    (46.)
                  )
                )
                 (distance p1 p2)
               )
             )
        )
      )
      (vl-catch-all-apply (function (lambda () (vla-explode block) (vla-delete block))))
    )
     (vla-insertblock
       (if
         (or (eq acmodelspace
                 (vla-get-activespace
                   (cond (*AcadDoc*)
                         ((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object))))
                   )
                 )
             )
             (eq :vlax-true (vla-get-mspace *AcadDoc*))
         )
          (vla-get-modelspace *AcadDoc*)
          (vla-get-paperspace *AcadDoc*)
       )
       (vlax-3d-point (trans p1 1 0))
       blockname
       1.
       1.
       1.
       (angle (trans p1 1 0) (trans p2 1 0))
     )
   )
 )
 (princ)
)

Link to comment
Share on other sites

I'm not really following this thread but when modifying dynamic blocks (in AutoCAD) they get their anonymous name. Will that have any affect on anything you do using code?

Link to comment
Share on other sites

I'm not really following this thread but when modifying dynamic blocks (in AutoCAD) they get their anonymous name. Will that have any affect on anything you do using code?

 

Same as if you altered it by the Dynamic grips.

Link to comment
Share on other sites

You the man!! This is exactly what I'm looking for. Thanks

 

You're welcome. Happy to help. :)

 

Check is in the mail? :wink:

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