Nikon Posted April 17 Posted April 17 (edited) This code allows you to select the height of the pyramid by pointing to the text in the drawing. How can I change the code to select a dimension in the drawing to use the dimension value as the height of the truncated pyramid? (defun c:PyramidConeVolumeT (/ ent obj txt radius area1 area2 volume insPoint) (vl-load-com) (defun getHeightFromText () (setq ent (entsel "\nSelect the text object containing the height: ")) (setq obj (vlax-ename->vla-object (car ent))) (setq txt (vla-get-TextString obj)) (setq height (distof txt 2)) ) (defun getCircleArea (circle) (setq radius (vla-get-Radius circle)) (* pi radius radius) ) (defun selectBase () (setq ent (entsel "\nSelect a polyline or circle as the base: ")) (setq obj (vlax-ename->vla-object (car ent))) (if (eq (vla-get-ObjectName obj) "AcDbCircle") (getCircleArea obj) (vlax-curve-getArea obj) ) ) (setq area1 (selectBase)) (princ (strcat "\nArea A1: " (rtos area1 2 3))) (setq area2 (selectBase)) (princ (strcat "\nArea A2: " (rtos area2 2 3))) (getHeightFromText) (princ (strcat "\nHeight H: " (rtos height 2 3))) ;; Calculating the volume in mm3 (setq volume (* (/ height 3) (+ area1 (sqrt (* area1 area2)) area2))) ;; Converting volume to m3 (setq volume_m3 (/ volume 1e9)) (princ (strcat "\nThe volume of a truncated pyramid or cone (m3): " (rtos volume_m3 2 3))) (setq insPoint (getpoint "\nSpecify the insertion point of the result: ")) (command "_.TEXT" insPoint "2.5" "0" (strcat "Volume (m3): " (rtos volume_m3 2 3))) (princ) ) Edited April 17 by Nikon Quote
GLAVCVS Posted April 17 Posted April 17 (edited) Hi Replacing your function with this one... is that enough? (defun getHeightFromText () (setq ent (entsel "\nSelect the text object containing the height: ")) (setq obj (vlax-ename->vla-object (car ent))) (if (= (setq to (CDR (ASSOC 0 (entget (car ent))))) "TEXT") (setq txt (vla-get-TextString obj)) (if (= to "DIMENSION") (if (= (setq txt (vla-get-textOverride obj)) "") (setq txt (rtos (vla-get-measurement obj) 2 2)) txt ) (progn (alert "No valid object") nil) ) ) (setq height (distof txt 2)) ) Edited April 17 by GLAVCVS 1 Quote
GLAVCVS Posted April 17 Posted April 17 You may need to copy the code again: I changed something. 1 Quote
Nikon Posted April 17 Author Posted April 17 (edited) On 4/17/2025 at 1:42 PM, GLAVCVS said: You may need to copy the code again: I changed something. I'm missing something... The code works on the first download. When called again, the command does not work. PYRAMIDCONEVOLUMETDIM GETHEIGHTFROMTEXT??? (defun c:PyramidConeVolumeTDIM (/ ent obj txt radius area1 area2 volume insPoint) (vl-load-com) (defun getHeightFromText () (setq ent (entsel "\nSelect the text object containing the height: ")) (setq obj (vlax-ename->vla-object (car ent))) (if (= (setq to (CDR (ASSOC 0 (entget (car ent))))) "TEXT") (setq txt (vla-get-TextString obj)) (if (= to "DIMENSION") (if (= (setq txt (vla-get-textOverride obj)) "") (setq txt (rtos (vla-get-measurement obj) 2 2)) txt ) (progn (alert "No valid object") nil) ) ) (setq height (distof txt 2)) ) ) (defun getCircleArea (circle) (setq radius (vla-get-Radius circle)) (* pi radius radius) ) (defun selectBase () (setq ent (entsel "\nSelect a polyline or circle as the base: ")) (setq obj (vlax-ename->vla-object (car ent))) (if (eq (vla-get-ObjectName obj) "AcDbCircle") (getCircleArea obj) (vlax-curve-getArea obj) ) ) (setq area1 (selectBase)) (princ (strcat "\nArea A1: " (rtos area1 2 3))) (setq area2 (selectBase)) (princ (strcat "\nArea A2: " (rtos area2 2 3))) (getHeightFromText) (princ (strcat "\nHeight H: " (rtos height 2 3))) ;; Calculating the volume in mm3 (setq volume (* (/ height 3) (+ area1 (sqrt (* area1 area2)) area2))) ;; Converting volume to m3 (setq volume_m3 (/ volume 1e9)) (princ (strcat "\nThe volume of a truncated pyramid or cone (m3): " (rtos volume_m3 2 3))) (setq insPoint (getpoint "\nSpecify the insertion point of the result: ")) (command "_.TEXT" insPoint "2.5" "0" (strcat "Volume (m3): " (rtos volume_m3 2 3))) (princ) ) Edited April 19 by Nikon Quote
GLAVCVS Posted April 17 Posted April 17 An example drawing will help to find a solution faster. Quote
Nikon Posted April 17 Author Posted April 17 (edited) 8 minutes ago, GLAVCVS said: An example drawing will help to find a solution faster. PyramidConeVolumeT.dwg Edited April 17 by Nikon Quote
GLAVCVS Posted April 17 Posted April 17 52 minutes ago, Nikon said: I'm missing something... The code works on the first download. When the command is called again, it appears. When called again, the command does not work. PYRAMIDCONEVOLUMETDIM GETHEIGHTFROMTEXT??? (defun c:PyramidConeVolumeTDIM (/ ent obj txt radius area1 area2 volume insPoint) (vl-load-com) (defun getHeightFromText () (setq ent (entsel "\nSelect the text object containing the height: ")) (setq obj (vlax-ename->vla-object (car ent))) (if (= (setq to (CDR (ASSOC 0 (entget (car ent))))) "TEXT") (setq txt (vla-get-TextString obj)) (if (= to "DIMENSION") (if (= (setq txt (vla-get-textOverride obj)) "") (setq txt (rtos (vla-get-measurement obj) 2 2)) txt ) (progn (alert "No valid object") nil) ) ) (setq height (distof txt 2)) ) ) (defun getCircleArea (circle) (setq radius (vla-get-Radius circle)) (* pi radius radius) ) (defun selectBase () (setq ent (entsel "\nSelect a polyline or circle as the base: ")) (setq obj (vlax-ename->vla-object (car ent))) (if (eq (vla-get-ObjectName obj) "AcDbCircle") (getCircleArea obj) (vlax-curve-getArea obj) ) ) (setq area1 (selectBase)) (princ (strcat "\nArea A1: " (rtos area1 2 3))) (setq area2 (selectBase)) (princ (strcat "\nArea A2: " (rtos area2 2 3))) (getHeightFromText) (princ (strcat "\nHeight H: " (rtos height 2 3))) ;; Calculating the volume in mm3 (setq volume (* (/ height 3) (+ area1 (sqrt (* area1 area2)) area2))) ;; Converting volume to m3 (setq volume_m3 (/ volume 1e9)) (princ (strcat "\nThe volume of a truncated pyramid or cone (m3): " (rtos volume_m3 2 3))) (setq insPoint (getpoint "\nSpecify the insertion point of the result: ")) (command "_.TEXT" insPoint "2.5" "0" (strcat "Volume (m3): " (rtos volume_m3 2 3))) (princ) ) You've moved a lot of things around in this code compared to the original you posted. So it's hard to get it to work. 1 Quote
GLAVCVS Posted April 17 Posted April 17 Here: your code modified to make it work. But there's one problem: you haven't specified the volume calculations correctly. I think I should leave this to you. (defun c:PyramidConeVolumeT (/ ent obj txt radius area1 area2 volume insPoint to) (vl-load-com) (defun getHeightFromText () (setq ent (entsel "\nSelect the text object containing the height: ")) (setq obj (vlax-ename->vla-object (car ent))) (if (= (setq to (CDR (ASSOC 0 (entget (car ent))))) "TEXT") (setq txt (vla-get-TextString obj)) (if (= to "DIMENSION") (if (= (setq txt (vla-get-textOverride obj)) "") (setq txt (rtos (vla-get-measurement obj) 2 2)) txt ) (progn (alert "No valid object") nil) ) ) (setq height (distof txt 2)) ) (defun getCircleArea (circle) (setq radius (vla-get-Radius circle)) (* pi radius radius) ) (defun selectBase () (setq ent (entsel "\nSelect a polyline or circle as the base: ")) (setq obj (vlax-ename->vla-object (car ent))) (if (eq (vla-get-ObjectName obj) "AcDbCircle") (getCircleArea obj) (vlax-curve-getArea obj) ) ) (setq area1 (selectBase)) (princ (strcat "\nArea A1: " (rtos area1 2 3))) (setq area2 (selectBase)) (princ (strcat "\nArea A2: " (rtos area2 2 3))) (getHeightFromText) (princ (strcat "\nHeight H: " (rtos height 2 3))) ;; Calculating the volume in mm3 (setq volume (* (/ height 3) (+ area1 (sqrt (* area1 area2)) area2))) ;; Converting volume to m3 (setq volume_m3 (/ volume 1e9)) (princ (strcat "\nThe volume of a truncated pyramid or cone (m3): " (rtos volume_m3 2 3))) (setq insPoint (getpoint "\nSpecify the insertion point of the result: ")) (command "_.TEXT" insPoint "2.5" "0" (strcat "Volume (m3): " (rtos volume_m3 2 3))) (princ) ) 1 Quote
Nikon Posted April 17 Author Posted April 17 (edited) 8 minutes ago, GLAVCVS said: Here: your code modified to make it work. But there's one problem: you haven't specified the volume calculations correctly. I think I should leave this to you. It works perfectly! Thank you very much! I'll check the formula... Edited April 17 by Nikon Quote
Nikon Posted April 17 Author Posted April 17 (edited) 3 hours ago, GLAVCVS said: But there's one problem: you haven't specified the volume calculations correctly. If you hurry, you'll make people laugh... I am replacing the formula string with (setq volume (/ (* height (+ area1 area2 (sqrt (* area1 area2)))) 3)) The code now works like a Swiss watch. ;; Thanks GLAVCVS 17.04.2025 ;; https://www.cadtutor.net/forum/topic/97474-select-a-dimension-value-as-the-height-of-the-pyramid/ ;; Calculating the volume of a truncated pyramid or a truncated cone /_\ \_/ (defun c:PyramidConeVolumeTDIM (/ ent obj txt radius area1 area2 volume insPoint to) (vl-load-com) (defun getHeightFromText () (setq ent (entsel "\nSelect the text object containing the height: ")) (setq obj (vlax-ename->vla-object (car ent))) (if (= (setq to (CDR (ASSOC 0 (entget (car ent))))) "TEXT") (setq txt (vla-get-TextString obj)) (if (= to "DIMENSION") (if (= (setq txt (vla-get-textOverride obj)) "") (setq txt (rtos (vla-get-measurement obj) 2 2)) txt ) (progn (alert "No valid object") nil) ) ) (setq height (distof txt 2)) ) (defun getCircleArea (circle) (setq radius (vla-get-Radius circle)) (* pi radius radius) ) (defun selectBase () (setq ent (entsel "\nSelect a polyline or circle as the base: ")) (setq obj (vlax-ename->vla-object (car ent))) (if (eq (vla-get-ObjectName obj) "AcDbCircle") (getCircleArea obj) (vlax-curve-getArea obj) ) ) (setq area1 (selectBase)) (princ (strcat "\nArea A1: " (rtos area1 2 3))) (setq area2 (selectBase)) (princ (strcat "\nArea A2: " (rtos area2 2 3))) (getHeightFromText) (princ (strcat "\nHeight H: " (rtos height 2 3))) ;; Calculating the volume in mm3 (setq volume (/ (* height (+ area1 area2 (sqrt (* area1 area2)))) 3)) (setq volume_m3 (/ volume 1e9)) (princ (strcat "\nThe volume of a truncated pyramid or cone (m3): " (rtos volume_m3 2 3))) (setq insPoint (getpoint "\nSpecify the insertion point of the result: ")) (command "_.TEXT" insPoint "2.5" "0" (strcat "Volume (m3): " (rtos volume_m3 2 3))) (princ) ) Edited April 17 by Nikon 1 Quote
Recommended Posts
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.