Jump to content

create a point at the centroid of a region


Leonardo.cunha

Recommended Posts

Hello everybody

in some searches on the internet I found this lsp that gives the location of the centroid of a region.

Would it be possible to use it so that Autocad inserts a point in the centroid of the region?

 

(defun c:centroid()
(cond
((not(setq
app (vlax-get-acad-object)
doc (vla-get-activedocument app)
ss (ssget ":S" '((0 . "3dsolid,body,region")))
)
)
(vlax-release-object app)
(princ "\nNo valid modeling object selected. ")
(princ)
)
((not (setq
obj (vlax-ename->vla-object (ssname ss 0))
test (vlax-property-available-p obj 'centroid)
)
)
(vlax-release-object app)
(vlax-release-object doc)
(vlax-release-object obj)
(princ "\nCentroid property not available for selected object. ")
(princ)
)
(T
(trans (vlax-safearray->list (vlax-variant-value (vla-get-centroid
obj))) (ssname ss 0) 1)
)
)
)

I don't have programming knowledge to do this, can you help me?

Thanks

Link to comment
Share on other sites

Quick one to start with, if you hit the <> before you post code and paste your code into the pop-up box it puts it into a shaded box - much easier to read where the code starts and stops.

 

 

A lesson in LISPs then,,,, what output does this give you - often it will tell you stuff in the command line. Run it and what does it say?

Link to comment
Share on other sites

Try this

(defun c:centroid( / ss obj )

(setq ss (ssget "+.:E:S" '((0 . "3dsolid,body,region"))))
(if (= ss nil)
  (princ "\nNo valid modeling object selected. ")
  (progn
    (setq obj (vlax-ename->vla-object (ssname ss 0)))
    (if (vlax-property-available-p obj 'centroid)
      (Command "point" (trans (vlax-safearray->list (vlax-variant-value (vla-get-centroid obj))) (ssname ss 0) 1))
      (princ "\nCentroid property not available for selected object. ")
    )
  )
)

(princ)
)

 

Link to comment
Share on other sites

6 hours ago, Leonardo.cunha said:

Hello everybody

in some searches on the internet I found this lsp that gives the location of the centroid of a region.

Would it be possible to use it so that Autocad inserts a point in the centroid of the region?

 

(defun c:centroid()
(cond
((not(setq
app (vlax-get-acad-object)
doc (vla-get-activedocument app)
ss (ssget ":S" '((0 . "3dsolid,body,region")))
)
)
(vlax-release-object app)
(princ "\nNo valid modeling object selected. ")
(princ)
)
((not (setq
obj (vlax-ename->vla-object (ssname ss 0))
test (vlax-property-available-p obj 'centroid)
)
)
(vlax-release-object app)
(vlax-release-object doc)
(vlax-release-object obj)
(princ "\nCentroid property not available for selected object. ")
(princ)
)
(T
(trans (vlax-safearray->list (vlax-variant-value (vla-get-centroid
obj))) (ssname ss 0) 1)
)
)
)

I don't have programming knowledge to do this, can you help me?

Thanks

 

5 hours ago, Steven P said:

Quick one to start with, if you hit the <> before you post code and paste your code into the pop-up box it puts it into a shaded box - much easier to read where the code starts and stops.

 

 

A lesson in LISPs then,,,, what output does this give you - often it will tell you stuff in the command line. Run it and what does it say?

Thanks for the tip, I'll use <> next time

  • Like 1
Link to comment
Share on other sites

3 hours ago, BIGAL said:

Try this

(defun c:centroid( / ss obj )

(setq ss (ssget "+.:E:S" '((0 . "3dsolid,body,region"))))
(if (= ss nil)
  (princ "\nNo valid modeling object selected. ")
  (progn
    (setq obj (vlax-ename->vla-object (ssname ss 0)))
    (if (vlax-property-available-p obj 'centroid)
      (Command "point" (trans (vlax-safearray->list (vlax-variant-value (vla-get-centroid obj))) (ssname ss 0) 1))
      (princ "\nCentroid property not available for selected object. ")
    )
  )
)

(princ)
)

Perfect, this is exactly what I was looking for.

Thank you very much

 

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