Jump to content

dynamic block parameter to Mleader issue


EYNLLIB

Recommended Posts

I'm almost there with my workflow, but I need a little help to get it just right.

 

Here's what I'm trying to do:

 

  1. Type command
  2. Select block
  3. Pick Mleader arrow location
  4. Pick Mleader landing location
  5. Mleader content should automatically populate with the "SW_LENGTH" parameter from the block

 

Everything works great up until step 5. After I pick both Mleader points, the Mleader text box opens for me to fill out (it's blank). But if I click in the drawing area, it exits the text box and fills in the correct info.

So, how do I skip that last step and have the Mleader content fill in automatically without Autocad opening the text edit box?

 

 

 

 

;; Get Dynamic Block Property Value  -  Lee Mac
;; Returns the value of a Dynamic Block property (if present)
;; blk - [vla] VLA Dynamic Block Reference object
;; prp - [str] Dynamic Block property name (case-insensitive)

(defun LM:getdynpropvalue (blk prp)
  (setq prp (strcase prp))
  (vl-some '(lambda (x) 
              (if (= prp (strcase (vla-get-propertyname x))) 
                  (vlax-get x 'Value)))
            (vlax-invoke blk 'GetDynamicBlockProperties)))

(defun c:swtest ()
  (setq blockName (car (entsel "\nSelect Block: ")))
  (if (and blockName (eq (cdr (assoc 0 (entget blockName))) "INSERT"))
      (progn
        (setq vlaBlock (vlax-ename->vla-object blockName))
        (setq SW_LENGTH (LM:getdynpropvalue vlaBlock "SW_LENGTH"))
        (if SW_LENGTH
            (progn
              (setq textString (strcat "SW_LENGTH: " (rtos SW_LENGTH 2 2)))
              (princ (strcat "\n" textString))
              
              (if (setq ins (getpoint "\nSpecify start point for MLeader: "))
                  (progn
                    (setq endPoint (getpoint "\nSpecify end point for MLeader: " ins))
                    (setq curlay (getvar "CLAYER"))
                    (setvar 'CMDECHO 0)
                    (command "_.undo" "_group") 
                    (setvar 'CLAYER "S - TEXT")
                    (command "CMLEADERSTYLE" "NORMAL - SW")
                    (setvar 'CMDECHO 1)
                    (initcommandversion)
                    
                    (command ".MLeader" ins endPoint "")
                    (while (> (getvar "CMDACTIVE") 0)
                      (command PAUSE))
                    
                    (setq mleaderObj (vlax-ename->vla-object (entlast)))
                    (vla-put-TextString mleaderObj textString)
                    
                    (setvar 'CMDECHO 0)
                    (command "CMLEADERSTYLE" "Normal")
                    (command "_.LAYER" "_SET" curlay "")
                    (command "_.undo" "_end") 
                    (setvar 'CMDECHO 1)
                    (princ "MLeader with SW_LENGTH created."))
                (alert "Insertion point not specified!")))
          (alert "SW_LENGTH attribute not found!")))
    (alert "Selected entity is not a block!"))
  (princ))

 

Link to comment
Share on other sites

For reference, I have a similar routine that grabs the length of a polyline and appends the mleader content with some custom text and the length that works perfectly;

 

 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CUSTOM FUNCTIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defun my-mod (x y)
  (- x (* y (fix (/ x y))))
)

(defun dot-product (vec1 vec2)
  (+ (* (nth 0 vec1) (nth 0 vec2))
     (* (nth 1 vec1) (nth 1 vec2)))
)

(defun list->points (lst / result x y)
  (setq result '())
  (while lst
    (setq x (car lst)
          y (cadr lst)
          lst (cddr lst))
    (setq result (append result (list (list x y))))
  )
  result
)

(defun distance (pt1 pt2)
  (sqrt (+ (expt (- (car pt2) (car pt1)) 2)
           (expt (- (cadr pt2) (cadr pt1)) 2)))
)

(defun distance-to-segment (p a b)
  (setq l2 (distance a b))
  (if (= l2 0.0)
    (distance p a)
    (progn
      (setq denominator (dot-product (mapcar '- b a) (mapcar '- p a)))
      (setq temp (/ denominator (dot-product (mapcar '- b a) (mapcar '- b a))))
      (setq projection (mapcar '+ a (mapcar '* (mapcar '- b a) (list temp temp temp))))
      (distance p projection)
    )
  )
)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; END CUSTOM FUNCTIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;











;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; MAIN PROGRAM ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defun c:sw ()
  (initget 1 "1 2 3 4 5")  
  (setq userNumber (getint "\nEnter Shearwal Type (1-5): "))
  (if userNumber
    (progn
      (setq selectedEntity (entsel "\nSelect a polyline: "))      
      (if (or (null selectedEntity)
              (/= "LWPOLYLINE" (cdr (assoc 0 (entget (car selectedEntity))))))
        (progn
          (princ "\nCancelled or not a polyline.")
          (exit)
        )
      )
      
      (setq pickedPoint (cadr selectedEntity))      
      (setq polyline (vlax-ename->vla-object (car selectedEntity)))      
      (setq vertices (vlax-get polyline 'Coordinates))      
      (setq vertices (list->points vertices))      
      (setq numVertices (length vertices))
      (setq closestSegment nil)

      (setq i 0)
      (while (< i numVertices)
        (setq pt1 (nth i vertices))
        (setq pt2 (nth (my-mod (+ i 1) numVertices) vertices))        
        (setq distToSegment (distance-to-segment pickedPoint pt1 pt2))
        
        (if (or (null closestSegment) (< distToSegment (car closestSegment)))
          (setq closestSegment (cons distToSegment (list pt1 pt2)))
        )        
        
        (setq i (+ i 1))
      )
      
      (setq segmentlength (distance (nth 0 (cdr closestSegment)) (nth 1 (cdr closestSegment))))      
      
      (setq roundedLength (fix (+ 0.5 segmentlength)))
      (setq feet (fix (/ roundedLength 12)))
      (setq inches (rem roundedLength 12))
      (setq str (strcat "SW" (itoa userNumber) " (" (itoa feet) "'-" (itoa inches) "\")"))

      (if (setq ins (getpoint "\nSpecify start point for mleader: "))
          (progn            
            (setq endPoint (getpoint "\nSpecify end point for mleader: " ins))         
            (setq curlay (getvar "CLAYER"))          
            (setvar 'CMDECHO 0)
            (setvar 'CLAYER "S - TEXT")
            (command "cmleaderstyle" "Normal - SW")
            (setvar 'CMDECHO 1)            
            (setq mleaderObj
                  (vlax-invoke
                  (vlax-get (vla-get-ActiveDocument (vlax-get-acad-object))
                            (if (= 1 (getvar 'cvport))
                                'PaperSpace
                              'ModelSpace
                              )
                            )
                  'AddMLeader
                  (append ins endPoint)
                  0
                  )
                  )
            
            (vla-put-TextString mleaderObj str)   
            (setvar 'CMDECHO 0)            
            (command "REGEN")         
            (setvar 'CLAYER curlay)         
            (command "cmleaderstyle" "Normal")
            (setvar 'CMDECHO 1)
            )
        )
      )
    )
  (princ (strcat "\nExact Length of Shearwall: " (rtos segmentlength))) ; Print the shearwall length
  (princ)
)

 

Link to comment
Share on other sites

as an update, i fixed the code using VLAX-INVOKE method to create the mleader:

 

 

it's friday...give me a break :D

 

 

(defun LM:getdynpropvalue (blk prp)
  (setq prp (strcase prp))
  (vl-some '(lambda (x) 
              (if (= prp (strcase (vla-get-propertyname x))) 
                  (vlax-get x 'Value)))
            (vlax-invoke blk 'GetDynamicBlockProperties)))

(defun c:swtest ()
  (setq blockName (car (entsel "\nSelect Block: ")))
  (if (and blockName (eq (cdr (assoc 0 (entget blockName))) "INSERT"))
      (progn
        (setq vlaBlock (vlax-ename->vla-object blockName))
        (setq SW_LENGTH (LM:getdynpropvalue vlaBlock "SW_LENGTH"))
        (if SW_LENGTH
            (progn
              (setq textString (strcat "SW_LENGTH: " (rtos SW_LENGTH 2 2)))
              (princ (strcat "\n" textString))
              
              (if (setq ins (getpoint "\nSpecify start point for MLeader: "))
                  (progn
                    (setq endPoint (getpoint "\nSpecify end point for MLeader: " ins))
                    (setq curlay (getvar "CLAYER"))
                    (setvar 'CMDECHO 0)
                    (command "_.undo" "_group") 
                    (setvar 'CLAYER "S - TEXT")
                    (command "CMLEADERSTYLE" "NORMAL - SW")
                    (setvar 'CMDECHO 1)
                    (initcommandversion)
                    
                    ;; Creating MLeader using vlax-invoke method
                    (setq mleaderObj
                          (vlax-invoke
                           (vlax-get
                            (vla-get-ActiveDocument (vlax-get-acad-object))
                            (if (= 1 (getvar 'cvport))
                                'PaperSpace
                              'ModelSpace))
                           'AddMLeader
                           (append ins endPoint)
                           0))
                    
                    (vla-put-TextString mleaderObj textString)
                    
                    (setvar 'CMDECHO 0)
                    (command "CMLEADERSTYLE" "Normal")
                    (command "_.LAYER" "_SET" curlay "")
                    (command "_.undo" "_end") 
                    (setvar 'CMDECHO 1)
                    (princ "MLeader with SW_LENGTH created."))
                (alert "Insertion point not specified!")))
          (alert "SW_LENGTH attribute not found!")))
    (alert "Selected entity is not a block!"))
  (princ))

 

  • Like 1
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...