RepCad Posted April 2, 2020 Posted April 2, 2020 Hi all, I wrote some code to get total area of selected hatches in autocad, it works well for low selection set, but when I select more hatches, it gives this error : ; error: AutoCAD.Application: Not applicable What am I doing wrong? (defun c:hat () (VL-LOAD-COM) (setq ss (ssget '((0 . "Hatch")))) (setq kk -1) (repeat (sslength ss) (setq kk (1+ kk)) (setq e (ssname ss kk)) (setq en (vlax-ename->vla-object e)) (setq ar (vlax-get en 'Area)) (setq tot (+ tot ar)) ) (alert (strcat "Total Area:" " " (rtos tot 2 3))) ) Quote
marko_ribar Posted April 2, 2020 Posted April 2, 2020 Not an answer to your question, but correction (remark)... ... (setq kk -1) (setq tot 0.0) (repeat ... ... 2 Quote
Jonathan Handojo Posted April 2, 2020 Posted April 2, 2020 Idk if it will solve your problem but... (defun c:hat ( / ss) (if (setq ss (ssget '((0 . "HATCH")))) (alert (strcat "Total Area: " (rtos (apply '+ (mapcar 'vla-get-Area (JH:selset-to-list-vla ss))) 2 3))) ) ) (defun JH:selset-to-list-vla (selset / lst i) ; Returns all entities within a selection set into a list of vla-objects. (repeat (setq i (sslength selset)) (setq lst (cons (vlax-ename->vla-object (ssname selset (setq i (1- i)))) lst)) ) (reverse lst) ) 1 Quote
RepCad Posted April 2, 2020 Author Posted April 2, 2020 11 minutes ago, marko_ribar said: Not an answer to your question, but correction (remark)... ... (setq kk -1) (setq tot 0.0) (repeat ... ... The line is added, but it gives same error : (defun c:hat () (VL-LOAD-COM) (setq ss (ssget '((0 . "Hatch")))) (setq kk -1) (setq tot 0.0) (repeat (sslength ss) (setq kk (1+ kk)) (setq e (ssname ss kk)) (setq en (vlax-ename->vla-object e)) (setq ar (vlax-get en 'Area)) (setq tot (+ tot ar)) ) (alert (strcat "Total Area:" " " (rtos tot 2 3))) ) Quote
tombu Posted April 2, 2020 Posted April 2, 2020 I've used Lee Mac's http://www.lee-mac.com/areastofield.html for years works on hatches as well as closed objects. Quote
RepCad Posted April 2, 2020 Author Posted April 2, 2020 2 hours ago, marko_ribar said: Not an answer to your question, but correction (remark)... ... (setq kk -1) (setq tot 0.0) (repeat ... ... I have found the problem, some hatches don't have area and for this reason the program stop and gives the Not applicable error. Quote
BIGAL Posted April 3, 2020 Posted April 3, 2020 Wrap your tot in a if so if not exist area skip. 1 Quote
ronjonp Posted April 4, 2020 Posted April 4, 2020 1 hour ago, BIGAL said: Wrap your tot in a if so if not exist area skip. Then return a false total ? Quote
BIGAL Posted April 4, 2020 Posted April 4, 2020 (edited) (setq ar (vlax-get en 'Area)) (if ar ?? (setq tot (+ tot ar)) ) need a zero hatch area to test Edited April 4, 2020 by BIGAL 1 Quote
RepCad Posted April 4, 2020 Author Posted April 4, 2020 6 hours ago, BIGAL said: (setq ar (vlax-get en 'Area)) (if ar ?? (setq tot (+ tot ar)) ) need a zero hatch area to test I tried to skip the error with "vl-catch-all-error-p', but I couldn't. Quote
dlanorh Posted April 4, 2020 Posted April 4, 2020 3 hours ago, amir0914 said: I tried to skip the error with "vl-catch-all-error-p', but I couldn't. Post a dxf file or AutoCAD 2010 or earlier sample drawing containing hatches and hatches without areas 1 Quote
Lee Mac Posted April 4, 2020 Posted April 4, 2020 (edited) The unavailability of the ActiveX Area property for a Hatch object is typically a result of the hatch having a self-intersecting boundary. Whilst you could easily circumvent the error that you are receiving using a vl-catch-all-apply expression, this will simply skip over those hatches for which the area property is unavailable and so the total area returned will be inaccurate. Edited April 4, 2020 by Lee Mac 1 Quote
RepCad Posted April 4, 2020 Author Posted April 4, 2020 5 hours ago, dlanorh said: Post a dxf file or AutoCAD 2010 or earlier sample drawing containing hatches and hatches without areas Test.Hatch.dwg Quote
BIGAL Posted April 6, 2020 Posted April 6, 2020 This is the area causing the problem no idea how to fix. You need to look at the original boundary. 1 Quote
Roy_043 Posted April 6, 2020 Posted April 6, 2020 FYI: BricsCAD does not have an issue determining the area of the hatch indicated by Bigal. 1 Quote
dlanorh Posted April 6, 2020 Posted April 6, 2020 (edited) Try this rework. Tested on your provided drawing. (defun c:hat (/ ss cnt obj ar tot) (setq tot 0 ss (ssget '((0 . "HATCH")))) (cond (ss (repeat (setq cnt (sslength ss)) (setq obj (vlax-ename->vla-object (setq ent (ssname ss (setq cnt (1- cnt)))))) (if (vl-catch-all-error-p (setq ar (vl-catch-all-apply 'vlax-get (list obj 'area)))) (progn (vl-cmdf "hatchgenerateboundary" ent "") (setq ar (getpropertyvalue (entlast) "area"))(entdel (entlast))) ) (setq tot (+ tot ar)) ) (alert (strcat "Total Area : "(rtos tot 2 3))) ) ) (princ) ) The hatch in question had three duplicate points at the start of the reinstated boundary polyline. Edited April 6, 2020 by dlanorh 1 1 Quote
RepCad Posted April 12, 2020 Author Posted April 12, 2020 On 4/6/2020 at 7:14 PM, dlanorh said: Try this rework. Tested on your provided drawing. (defun c:hat (/ ss cnt obj ar tot) (setq tot 0 ss (ssget '((0 . "HATCH")))) (cond (ss (repeat (setq cnt (sslength ss)) (setq obj (vlax-ename->vla-object (setq ent (ssname ss (setq cnt (1- cnt)))))) (if (vl-catch-all-error-p (setq ar (vl-catch-all-apply 'vlax-get (list obj 'area)))) (progn (vl-cmdf "hatchgenerateboundary" ent "") (setq ar (getpropertyvalue (entlast) "area"))(entdel (entlast))) ) (setq tot (+ tot ar)) ) (alert (strcat "Total Area : "(rtos tot 2 3))) ) ) (princ) ) The hatch in question had three duplicate points at the start of the reinstated boundary polyline. Thanks a lot dlanorh, that was very helpful and interesting. 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.