what i mistake select object change area lisp

what i mistake select object change area lisp

jaimuthu
Advocate Advocate
514 Views
7 Replies
Message 1 of 8

what i mistake select object change area lisp

jaimuthu
Advocate
Advocate
(defun c:ChangePolylineArea (/ ss obj area scaleFactor currentArea)
  (setq ss (ssget "_:L"))
  
  (if ss
    (progn
      (setq obj (ssname ss 0))
      (setq area 5.0) ; Desired area value

      (setq currentArea (vla-get-area (vlax-ename->vla-object obj)))
      (setq scaleFactor (sqrt (/ area currentArea)))

      (setq centroid (vlax-safearray->list (vlax-variant-value (vla-getboundingbox (vlax-ename->vla-object obj)))))
      (setq centerX (/ (+ (nth 0 centroid) (nth 2 centroid)) 2.0))
      (setq centerY (/ (+ (nth 1 centroid) (nth 3 centroid)) 2.0))

      (command "_.ucs" "_.object" obj "")
      (command "_.scale" obj "" "_non" centerX "," centerY scaleFactor)
      (princ "Area of selected polyline changed to 5.")
    )
    (princ "No polyline found.")
  )
  (princ)
)
0 Likes
Accepted solutions (2)
515 Views
7 Replies
Replies (7)
Message 2 of 8

devitg
Advisor
Advisor

@jaimuthu Please upload your sample DWG. 

0 Likes
Message 3 of 8

Kent1Cooper
Consultant
Consultant

Two things to start:

 

The (vla-getboundingbox) function requires two arguments for places to put the corners of it [as safearrays], and then you pull the point coordinate lists out of those, for example like this:

 

(vla-getboundingbox (vlax-ename->vla-object EntityName) 'minpt 'maxpt)
(setq
  LL (vlax-safearray->list minpt); Lower Left corner [coordinates list]
  UR (vlax-safearray->list maxpt); Upper Right
)

 

Then you calculate halfway between those LL and UR variables for your "centroid" [which, of course, may or may not actually be the centroid in the geometric sense].

 

Your base point in the Scale command is incorrect.  If you get the centroid correctly from the bounding box corners, you can just give it that, but if you have the X and Y coordinates separately, and want to supply the point in "X,Y" format, you would need to convert those numbers to text strings and combine them around the comma, something like this:

... "_non" (strcat (rtos centerX) "," (rtos centerY)) scaleFactor ...

Kent Cooper, AIA
0 Likes
Message 4 of 8

jaimuthu
Advocate
Advocate
(defun c:ChangePolylineArea (/ ss obj area scaleFactor currentArea minpt maxpt LL UR centerX centerY)
(setq ss (ssget "_:L"))

(if ss
(progn
(setq obj (ssname ss 0))
(setq area 5.0) ; Desired area value

(setq currentArea (vla-get-area (vlax-ename->vla-object obj)))
(setq scaleFactor (sqrt (/ area currentArea)))

(vla-getboundingbox (vlax-ename->vla-object obj) 'minpt 'maxpt)
(setq
LL (vlax-safearray->list minpt)
UR (vlax-safearray->list maxpt)
)

(setq
centerX (/ (+ (nth 0 LL) (nth 0 UR)) 2.0)
centerY (/ (+ (nth 1 LL) (nth 1 UR)) 2.0)
)

(setq basePoint (strcat (rtos centerX) "," (rtos centerY)))
(command "_.ucs" "_.object" obj "")
(command "_.scale" obj "" "_non" basePoint scaleFactor)
(princ "Area of selected polyline changed to 5.")
)
(princ "No polyline found.")
)
(princ)
)
NOT WORK
0 Likes
Message 5 of 8

Kent1Cooper
Consultant
Consultant
Accepted solution

@jaimuthu wrote:
....
(command "_.ucs" "_.object" obj "")
....
NOT WORK

"NOT WORK" is never enough information.  Does it not load the definition?  Does it not recognize the command name?  What happens, or doesn't happen, or happens differently than you expect?  Are there any messages?  Etc., etc.

 

My guess:  Remove the Enter [""] at the end of the UCS command quoted above.  The giving of the object to set the UCS to ends the command, so the "" will recall the last command prior to the AutoLisp routine.

 

If that at least makes it function, does the thing end up at the right size, but not centered in the same place?  If so, the change of the UCS is probably throwing off the Scale base point, because the bounding box will be in WCS coordinates, not current-UCS coordinates if you have changed that.  I would remove the UCS change, which will also eliminate the first problem.

 

If that's not the issue, explain why you are changing the UCS, and give us more information about what "NOT WORK" means.

Kent Cooper, AIA
0 Likes
Message 6 of 8

Sea-Haven
Mentor
Mentor
Accepted solution

A few goes with different syntax.

 

(command "ucs" "object" obj)
(command "_ucs" "_object" obj)

but this did not
(command "_.ucs" "_.object" obj)
Unable to recognize entry. Please try again.

 

 Ps just me I use ENT for entities  and OBJ for VL objects, others here also use this naming convention to quickly identify type of object.

0 Likes
Message 7 of 8

Kent1Cooper
Consultant
Consultant

@Sea-Haven wrote:
....
but this did not
(command "_.ucs" "_.object" obj)
Unable to recognize entry. Please try again.

.....


You can include the period as prefix to command names such as ucs, to force it to use the native command, in case there has been any redefinition.  [And/or the underscore so that English command and option names work in AutoCAD for any language.]  But the period does not belong as prefix to option words such as object.  That is probably the reason for the "Unable..." message.

Kent Cooper, AIA
0 Likes
Message 8 of 8

Sea-Haven
Mentor
Mentor

Yes that fixed it no "." on object

0 Likes