Author Topic: centroid lisp help  (Read 3847 times)

0 Members and 1 Guest are viewing this topic.

ronjonp

  • Needs a day job
  • Posts: 7529
centroid lisp help
« on: March 04, 2005, 06:47:39 PM »
Code: [Select]
(defun c:cent (/ ent obj obj-centroid)
  (vl-load-com)
(while
  (= ent nil)
  (setq ent (entsel "\n Select object to convert to region: "))
  (if (= ent nil)
  (princ "\n Nothing Selected!!")
  (command ".region" ent "")
  )
)
(setq ent (entsel "\n Select previously converted region to obtain centroid: "))
  (setq obj (vlax-ename->vla-object (car ent)))
    (setq obj-centroid (vlax-get obj 'centroid))
    (command ".circle" obj-centroid (* (getvar 'dimscale)0.1))
)


How do I obtain the newly created region without having to pick it?
I was trying to use (entget (entlast)) but as receiving errors.

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
centroid lisp help
« Reply #1 on: March 04, 2005, 08:40:21 PM »
See if this helps you out.....
Quote from: Code
(defun c:cent (/ doc ent_list idx regions space ss)
  (and (setq ss (ssget '((0 . "POLYLINE,LWPOLYLINE,LINE,ARC,CIRCLE,SPLINE"))))
       (setq idx 0)
       (setq ent_list (list (vlax-ename->vla-object (ssname ss idx))))
       (while (< (setq idx (1+ idx)) (sslength ss))
    (setq ent_list (append ent_list
            (list (vlax-ename->vla-object (ssname ss idx)))))
    )
       (setq doc (vla-get-activedocument (vlax-get-acad-object)))
       (not (vla-startundomark doc))
       (setq space (if (= (getvar "cvport") 1)
           (vla-get-paperspace doc)
           (vla-get-modelspace doc)))
       (setq regions (vlax-invoke space 'addregion ent_list))
       (foreach region regions
    (vlax-invoke space 'addcircle (append (vlax-get region 'centroid) '(0.0)) (* (getvar 'dimscale)0.1))
    )
       (not (vla-endundomark doc))
       (princ (strcat "\n.....Created " (itoa (length regions)) " region(s)!"))
       )  
  (princ)
  )


OK, there is code there if you really want to see it.......
but after looking at your code, just changing 2 lines worked for me:
Code: [Select]
(setq ent (entsel "\n Select previously converted region to obtain centroid: "))
     (setq obj (vlax-ename->vla-object (car ent)))
;;;;;CHANGE to:
(setq ent (entlast))
(setq obj (vlax-ename->vla-object ent))

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
centroid lisp help
« Reply #2 on: March 04, 2005, 09:45:31 PM »
'Cause Jeff's probably asleep, and the sun's shining on this side of the world ..

I'll propose something like this replacement to the start of Jeff's code .. Otherwise Single selections will get lost ..
Code: [Select]

;< .. >
  (and (setq idx      -1            
             ss       (ssget '((0 . "POLYLINE,LWPOLYLINE,LINE,ARC,CIRCLE,SPLINE")))
       )
       (repeat (sslength ss)
         (setq
           ent_list (append ent_list
                            (list (vlax-ename->vla-object (ssname ss (setq idx (1+ idx)))))
                    )
         )
       )
       (setq doc (vla-get;<..>
 
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
centroid lisp help
« Reply #3 on: March 04, 2005, 10:35:53 PM »
Quote from: Kerry Brown
'Cause Jeff's probably asleep, and the sun's shining on this side of the world ..
:lol:  :lol:  :lol:
I don't know how you folks on the bottom side of the planet do things, but up here in the atmosphere we don't go to bed when you can still see the light from the sun on the horizon.....sheesh, it's only 7:20 pm....haven't even had dinner or a cocktail yet.
Quote from: Kerry Brown

I'll propose something like this replacement to the start of Jeff's code .. Otherwise Single selections will get lost ..
You lost me here, Kerry. I initailize the list with the first object in the selection set THEN i go into my loop.......uhm, uh, nevermind. See, what I did was correct, other than being an (and)  argument that returns false, wrapping it in a (or (while.....) t) works too. I didn't do it the way Kerry shows it because I coulda swore that using (append) with a nil list caused it to error out. I just tried it though and it works fine....that's what I get for thinking on a Friday night.....

Thanks for the catch Kerry, and to think I said to myself "Test it with only a single object, too" "Nah, it'll do just fine"

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
centroid lisp help
« Reply #4 on: March 05, 2005, 04:35:46 AM »
Ooops, *Checks a map*
.. I thought you were on the other coast, my bad.

PS, thats a decent looking lake on your doorstep Jeff.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

t-bear

  • Guest
centroid lisp help
« Reply #5 on: March 05, 2005, 01:18:12 PM »
I believe Hendi has a sweet centroid proggy in the free downloads section over at Resource CAD International.  I'll look for it on Monday .........

ronjonp

  • Needs a day job
  • Posts: 7529
centroid lisp help
« Reply #6 on: March 07, 2005, 09:49:59 AM »
Thanks Jeff....as usual, I was making the problem much harder than it actually was.  :D

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC