Jump to content

Hatch area


RepCad

Recommended Posts

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

)

 

Screenshot (1478).png

Link to comment
Share on other sites

Not an answer to your question, but correction (remark)...

 

...

(setq kk -1)

(setq tot 0.0)

(repeat ...

...

  • Like 2
Link to comment
Share on other sites

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

 

  • Like 1
Link to comment
Share on other sites

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

)

 

Link to comment
Share on other sites

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.

Screenshot (1479).png

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

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 by Lee Mac
  • Like 1
Link to comment
Share on other sites

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 by dlanorh
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

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. 

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