Jump to content

Recommended Posts

Posted

Now that I'm (just starting) to get the hang of the syntax, I've almost got lisp doing what I want. But...

 

Through several revisions, I've arrived at something that ALMOST does what I want. The "get" portion does exactly what I want. My challenge right now is the "set" portion. I have blocks in my drawing defined with "CIRCUIT" and "ADDRESS" attributes. The goal is to give the function a circuit & address, and have it start from that number and automatically renumber all blocks with addresses equal to or greater than the start point.

 

I'm using Lee Mac's get/setattributevalue functions. I freely admit I don't fully understand their processing (yet) - because I don't (yet) understand AutoCAD entities - but I'm getting there.

 

My challenge - the call to setattributevalue gives me an error, specifically "bad function: " or whatever the handle might be. I don't understand because as far as I know that entity name is the same that worked fine for the getattributevalue function - unless my list processing has somehow invalidated the handle.

 

Anyone show me where I goofed?

 

(defun c:renumberaddresses ()

(setq circuit (getstring "\nEnter circuit:"))

(setq address (getint "\nEnter address:"))

 

(setq ln (DM:gatheraddresses circuit address))

(foreach e ln (progn

(setq blk (cadr e))

(setq newvalue (itoa address))

(LM:setattributevalue ( blk "ADDRESS" newvalue ))

(setq address (+ address 1))

))

(princ)

)

 

(defun DM:gatheraddresses ( circuit address / ssfilter ss numblocks i ln entityname entity_circuit entity_address e e1 e2 )

(setq ssfilter '((0 . "INSERT") (66 . 1))) ;define a filter to select all inserted blocks

(setq ss (ssget "_X" ssfilter)) ;query drawing using filter

(setq numblocks (sslength ss)) ;get number of blocks

 

(if (> numblocks 0) (progn ;assuming we found some blocks start...

(setq i 0) ;initialize i to 0

(setq ln (list)) ;initialize ln to null list

 

; loop through all the blocks found searching for matches

(repeat numblocks (progn ;loop through all blocks

(setq entityname (ssname ss i)) ;pop next entity to examine from selection set

 

;is this block on the right circuit?

(setq entity_circuit (LM:getattributevalue entityname "CIRCUIT"))

 

(if (= entity_circuit circuit) (progn

 

;does this block have an address equal or greater than the target?

(setq entity_address (LM:getattributevalue entityname "ADDRESS"))

 

(if (>= (atoi entity_address) address) (progn

(setq ln (cons (list entity_address entityname) ln))

))

))

 

(setq i (+ i 1))

))

 

; sort the list by address in ascending numerical order

(setq ln (vl-sort ln '(lambda (e1 e2) (

))

)

 

;; Get Attribute Value - Lee Mac

;; Returns the value held by the specified tag within the supplied block, if present.

;; blk - [ent] Block (Insert) Entity Name

;; tag - [str] Attribute TagString

;; Returns: [str] Attribute value, else nil if tag is not found.

 

(defun LM:getattributevalue ( blk tag / val enx )

(while

(and

(null val)

(= "ATTRIB" (cdr (assoc 0 (setq enx (entget (setq blk (entnext blk)))))))

)

(if (= (strcase tag) (strcase (cdr (assoc 2 enx))))

(setq val (cdr (assoc 1 enx)))

)

)

)

 

;; Set Attribute Value - Lee Mac

;; Sets the value of the first attribute with the given tag found within the block, if present.

;; blk - [ent] Block (Insert) Entity Name

;; tag - [str] Attribute TagString

;; val - [str] Attribute Value

;; Returns: [str] Attribute value if successful, else nil.

 

(defun LM:setattributevalue ( blk tag val / end enx )

(while

(and

(null end)

(= "ATTRIB" (cdr (assoc 0 (setq enx (entget (setq blk (entnext blk)))))))

)

(if (= (strcase tag) (strcase (cdr (assoc 2 enx))))

(if (entmod (subst (cons 1 val) (assoc 1 enx) enx))

(progn

(entupd blk)

(setq end val)

)

)

)

)

)

Posted

After a night's sleep...

 

(LM:setattributevalue ( blk "ADDRESS" newvalue )) doesn't work. Anyone who uses Lisp would recognize that. Since I'm still stuck on C type syntax...let's try:

 

(LM:setattributevalue blk "ADDRESS" newvalue )

and now the magic happens. Eventually I'll get the hang of this - and then I'll never be able to call functions in another language. Sigh.

Posted

Please read the Code Posting Guidelines and put your code in code tags.

[noparse]

Code goes here

[/noparse].

 

Should look like this...

 

Code goes here

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