Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 08/11/2025 in Posts

  1. Okay, this is the sub-function for calculating geometric center for open or closed polygon made from polyline. ; ******************************************************************************************* ; Sub-Function : SX:geomCenter ; Description : Geometric center for open or closed lwpolyline for 2D polygons ; Arguments: : ent - Selected entity into the drawing ; Calling function : (SX:geomCenter ent) ; Author : Saxlle ; Date : August 11, 2025 ; ******************************************************************************************* (defun SX:geomCenter ( ent / ptlist sum_x sum_y sum_area temp_area i len temp_x temp_y j cen_x cen_y cen_pt) (setq ptlist (mapcar 'cdr (vl-remove-if-not (function (lambda (x) (= (car x) 10))) (entget ent)))) ;; point list (if (and (equal (cdr (assoc 0 (entget ent))) "LWPOLYLINE") (>= (length ptlist) 3)) (progn (setq sum_x 0.0 sum_y 0.0 sum_area 0.0 temp_area 0.00 i 0 ) (repeat (setq len (length ptlist)) (if (< (1+ i) len) (progn (setq temp_area (- (* (car (nth i ptlist)) (cadr (nth (1+ i) ptlist))) (* (car (nth (1+ i) ptlist)) (cadr (nth i ptlist)))) sum_area (+ sum_area temp_area) temp_x (+ (car (nth i ptlist)) (car (nth (1+ i) ptlist))) temp_y (+ (cadr (nth i ptlist)) (cadr (nth (1+ i) ptlist))) sum_x (+ sum_x (* temp_x temp_area)) sum_y (+ sum_y (* temp_y temp_area)) i (1+ i) ) ) (progn (setq j 0 temp_area (- (* (car (nth i ptlist)) (cadr (nth j ptlist))) (* (car (nth j ptlist)) (cadr (nth i ptlist)))) sum_area (+ sum_area temp_area) temp_x (+ (car (nth i ptlist)) (car (nth j ptlist))) temp_y (+ (cadr (nth i ptlist)) (cadr (nth j ptlist))) sum_x (+ sum_x (* temp_x temp_area)) sum_y (+ sum_y (* temp_y temp_area)) ) ) ) ) (setq sum_area (/ sum_area 2)) (if (not (equal sum_area 0.0)) (setq cen_x (/ sum_x (* sum_area 6)) cen_y (/ sum_y (* sum_area 6)) cen_pt (list cen_x cen_y) ) ) (prompt (strcat "\nThe geometric center is: " (rtos cen_x 2 2) "," (rtos cen_y 2 2))) (princ) ) (progn (cond ((equal (cdr (assoc 0 (entget ent))) "LWPOLYLINE") (prompt "\nSelected entity is POLYLINE and the number of vertices are lower than 3! Need at least 3 or more vertices!") (princ) ) ((equal (cdr (assoc 0 (entget ent))) "LINE") (prompt "\nSelected entity is LINE and the center is the midpoint!") (princ) ) ) ) ) ) An example video of how it works. geomCenter.mp4 Best regards.
    3 points
  2. @Mahee , please give it a try , will work both for open and closed lwpolylines it get the Centroid from a temporary Region from a temporary closed polyline if poly is open get-center.LSP get-center.LSP get poly center.dwg
    2 points
  3. Ahh, that's my mistake, try this: (defun c:test (/ old_err EscVP StrScale2 TxtHt StrScale ss) (setq old_err *error*) (defun *error* ( a / ) ;; Add here return cmdecho to as it was, cmdecho 1 (princ "") (setq *error* old_err) (princ) ) ; End Error Defun (setvar "cmdecho" 0) ; perhaps record the state of cmdecho before here, and reset to that value at the end (if (and (setq EntVP (car (entsel "\Seleccione VIEWPORT: "))) (= (cdr (assoc 0 (entget EntVP))) "VIEWPORT") ) ; end and (progn (setq EscVP (vla-get-CustomScale (vlax-ename->vla-object EntVP))) (setq StrScale2 (rtos (* 3.5 (/ 1 EscVP)) 2 1)) (setq TxtHt (* 3.5 (/ 1 EscVP)) ) ; a number not a string ;; added this (setq StrScale (rtos (/ 1 EscVP) 2 2)) (setq vpnum (cdr (assoc 69 (entget EntVP )))) (vl-cmdf "_.mspace") (setvar "CVPORT" vpnum) (setq ss (ssget (list '(0 . "TEXT") '(-4 . "<>") (cons 40 TxtHt))) ) ;; corrected line ;;What are you doing with ss now? (Prompt (strcat "\nLa escala de La Ventana es 1/" StrScale)) (princ "\nLos textos de 3.5 en el interior de la ventana deben ser de: ") (princ StrScale2) ) ; end progn ) ; end if (setvar "cmdecho" 1) (princ) );fin defun
    1 point
  4. @maahee It is not clear what you want to calculate. Please respond to the following questions. Should the program be able to handle lines? ... lwpolylines without arc segments? ... lwpolylinese with arc segments? arc objects? circle objects? If the open/close property of a polyline is "open" sould the polyline be considered a closed shape defined by a line segment from the last vertex to the first vertex? The code in your last post dows nothing if the object is not a line. If it is a line it adds a dim line but does not identify the "center".
    1 point
  5. Very quickly.... code tag is <> , you used quote tag '' which makes reading your code harder You use sssetfirst as a variable - it is better practice to not use a command name also as a variable even if it works. for example: (setq sssetfirst (vla-get-activeselectionset (vla-get-activedocument (vlax-get-acad-object))) ) ;; A variable (sssetfirst nil (ssget "_WP" ob_lst_pt '((0 . "*TEXT,ATTDEF")(-4 . "<NOT") (62 . 4) (-4 . "NOT>")) )) ;; A command The next suggestion I would make for now is to separate out your code a bit - all good running lines together but while you are creating something go to simple programming methods, it often makes errors or bad code jump out: (sssetfirst nil (ssget "_WP" ob_lst_pt '((0 . "*TEXT,ATTDEF")(-4 . "<NOT") (62 . 4) (-4 . "NOT>")) )) could be (setq MySS (ssget "_WP" ob_lst_pt '((0 . "*TEXT,ATTDEF")(-4 . "<NOT") (62 . 4) (-4 . "NOT>")) ) ) (sssetfirst nil MySS) Next thing would be to add comments to the code especially if you are asking how things work, we can read through the code quickly and see the intent of how the code works Last thing, when asking for help - and we have no problem with that - give a clue what is not working as it should. I think that if you add comments to the code it should be clear what to change to make the colours also red. Perhaps one of you comments will be 'adjust text to size 3.5'... and that might be where you want to change the colour also.
    1 point
  6. @devitg@Saxlle There seems to be some dissagreement of the definition of the geometric center of a polyline. Should the polyline be considered a linear object or a closed shape? If it's a linear object then the geometric center for an open polyline as shown below is (1.5,0.8) and not (1.5,0.5). There is no line from (3,0) to (0,0) that influeces the location of the geometric cener. My code above considers the polyline as a series of lines and not the bounds of a shape (which is ill defined as it is open). There is also the interpretation of how to handle a polyline that is "open" but should be considered a closed shape where the last vertex is assumed to be the same as the first vertex. In this case the polyline needs to be tested for the "closed" property and then appropriately dealt with to define a closed shape.
    1 point
  7. The geometric center (a.k.a. centroid) of an open polyline is deteremined by summing the length of each line segment times the coordinates of the midpoint of the respective segment. The result is then divided by the total length of the polyline. It is NOT the average of the point locations. ; Calculates the centroid for an open lwpolyline ; lrm 8/9/2025 (defun c:pl_centroid (/ ss pt_lst sum_x sum_y sum_d n i d mid_pt centroid) (setq ss (entget (car (Entsel "\nSelect Open Polyline"))) pt_lst (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) ss) ) ) (setq sum_x 0 sum_y 0 sum_d 0.0 n (length pt_lst) i 0 ) (while (<= i (- n 2)) (setq d (distance (nth i pt_lst) (nth (+ i 1) pt_lst))) (setq mid_pt (mapcar '/ (mapcar '+ (nth i pt_lst) (nth (+ i 1) pt_lst)) '(2 2 2) ) ) (setq sum_x (+ sum_x (* (car mid_pt) d)) sum_y (+ sum_y (* (cadr mid_pt) d)) sum_d (+ d sum_d) i (+ i 1) ) ) (setq centroid (list (/ sum_x sum_d) (/ sum_y sum_d) ) ) (princ "\n Centroid at: ") (princ centroid) (command "_point" "_non" centroid) (princ) )
    1 point
  8. Hi I think it would be easier to find the problem if you attached an example drawing.
    1 point
×
×
  • Create New...