Jump to content

Recommended Posts

Posted

Hi All,

 

Can someone help with converting two attribute values to decimal from hexadecimal and then performing a simple mathematical operation on the two values and returning the result in the third attribute. The attached block has three attributes (AC, ADDRESS and DEV). The values for AC and ADDRESS are written in hexadecimal. The DEV value will be in decimal format. So, convert the AC value to decimal; convert the ADDRESS value to decimal; then add the two decimal values and return as the value in the DEV attribute. Any help would be greatly appreciated.

Hex2Dec.dwg

Posted

Try the following:

(defun c:hex2dec ( / a b c e i l s x )
   (if (setq s (ssget "_:L" '((0 . "INSERT") (2 . "RS") (66 . 1))))
       (repeat (setq i (sslength s))
           (setq e (entnext (ssname s (setq i (1- i))))
                 x (entget e)
           )
           (while (= "ATTRIB" (cdr (assoc 0 x)))
               (setq l (cons (list (strcase (cdr (assoc 2 x))) (cdr (assoc 1 x)) x) l)
                     e (entnext e)
                     x (entget  e)
               )
           )
           (if (and (setq a (assoc "AC" l))
                    (setq b (assoc "ADDRESS" l))
                    (setq c (assoc "DEV" l))
                    (entmod
                        (subst
                            (cons  1 (itoa (+ (LM:base->dec (cadr a) 16) (LM:base->dec (cadr b) 16))))
                            (assoc 1 (caddr c))
                            (caddr c)
                        )
                    )
               )
               (entupd (cdr (assoc -1 (caddr c))))
           )
       )
   )
   (princ)
)               

;; Base to Decimal  -  Lee Mac
;; Converts an number in an arbitrary base to decimal.
;; n - [str] string representing number to convert
;; b - [int] base of input string
;; Returns: [int] Decimal representation of supplied number

(defun LM:base->dec ( n b / l )
   (if (= 1 (setq l (strlen n)))
       (- (ascii n) (if (< (ascii n) 65) 48 55))
       (+ (* b (LM:base->dec (substr n 1 (1- l)) b)) (LM:base->dec (substr n l) b))
   )
)

(princ)

 

Base to Decimal from my set of Base Conversion Functions.

Posted

Thanks Lee,

 

But how and where do I load the function?

Posted

Thanks Lee,

 

Great stuff. But can I ask if we can expand on the simple addition. Rather than summing the values of the attributes AC and ADDRESS can we have the following.

DEV = 695164 +(((AC*632)+ADDRESS)*6). The values 695164, 632 and 6 are static values and will not change. I've tried various constructions in the line of code starting (cons 1 (itoa (+ (LM:base.........) but can't get it right.

Posted
can we have the following.

DEV = 695164 +(((AC*632)+ADDRESS)*6). The values 695164, 632 and 6 are static values and will not change.

 

Sure, change:

(cons  1 (itoa (+ (LM:base->dec (cadr a) 16) (LM:base->dec (cadr b) 16))))

to:

(cons  1 (itoa (+ 695164 (* 6 (+ (* (LM:base->dec (cadr a) 16) 632) (LM:base->dec (cadr b) 16))))))

Posted

Thanks Lee again. No reply to further query. Figured it out myself.

  • 4 years later...
Posted

Hi is there any way to do this in a table? I had an excel formula "=695164+((HEX2DEC($B2)*632)+HEX2DEC($C2))*6+1" but can't put this in to a cad table. I know I can link it in an excel table but I really need it to be fool proof for anyone editing the drawing.

Posted

Like others here, this is a example make a table lisp. There is no code re the convert hex2dec as that is answered earlier in the post.

; make table example
; By Alan H info@alanh.com.au
; 2018

(defun c:ahmaketable (/ colwidth numcolumns numrows objtable rowheight sp vgad vgao vgms)
(vl-load-com)
(setq sp (vlax-3d-point (getpoint "pick a point for table")))

(Setq vgms (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))) ;

(setq numrows 5)
(setq numcolumns 5)
(setq rowheight 2.5)
(setq colwidth 60)

(setq objtable (vla-addtable vgms sp numrows numcolumns rowheight colwidth))

(vla-settext objtable 0 0 "DRAWING REGISTER"); TABLE TITLE
(vla-settext objtable 1 0 "DRAWING NUMBER") 
(vla-settext objtable 1 1 "DRAWING TITLE") 
(vla-settext objtable 1 2 "C")
(vla-settext objtable 1 3 "D")
(vla-settext objtable 1 4 "E")
(vla-settext objtable 2 0 "1")
(vla-settext objtable 3 0 "2")
(vla-settext objtable 4 0 "3")

(command "_zoom" "e")

(princ)
)



You can then add rows to a table and fill in cells values using.

(setq obj2 (vlax-ename->vla-object (entlast))
(setq rownum (vla-get-rows obj2))
(vla-InsertRows obj2  rownum  (vla-GetRowHeight obj2 (- rownum 1)) 1)
(vla-settext obj2 rownum  colnum  txt)

 

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