Using CD AutoLISP in AutoCAD to divide an area

 




1. Understand the Goal

You want to divide a given area into smaller sections (equal or specified). This typically requires identifying boundaries, calculating the divisions, and creating the dividing lines or shapes.


2. Write the LISP Routine

Below is an example LISP code to divide a rectangular area into equal parts:

(defun c:DivideArea ()
  (prompt "\nSelect a closed polyline to divide: ")
  (setq obj (car (entsel))) ; Select the closed polyline
  (setq ent (entget obj)) ; Get entity data
  
  (if (= (cdr (assoc 0 ent)) "LWPOLYLINE") ; Check if it's a polyline
    (progn
      (setq num-div (getint "\nEnter the number of divisions: ")) ; Get divisions
      (setq area (vl-cmdf "_.AREA" obj)) ; Calculate area
      (setq pl (vlax-ename->vla-object obj)) ; Convert to object
      
      ;; Get the bounding box (extents)
      (vla-getboundingbox pl 'minPoint 'maxPoint)
      (setq minPt (vlax-safearray->list minPoint))
      (setq maxPt (vlax-safearray->list maxPoint))
      
      ;; Calculate dimensions
      (setq x-min (car minPt) y-min (cadr minPt))
      (setq x-max (car maxPt) y-max (cadr maxPt))
      
      ;; Define division logic (example: vertical lines)
      (setq step (/ (- x-max x-min) num-div))
      (repeat (1- num-div)
        (setq x (+ x-min (* step (setq count (1+ (or count 0))))))
        (entmake
          (list
            (cons 0 "LINE")
            (cons 10 (list x y-min 0))
            (cons 11 (list x y-max 0))
          )
        )
      )
      (prompt "\nArea divided successfully!")
    )
    (prompt "\nSelected object is not a closed polyline.")
  )
  (princ)
)

3. Load the LISP File

  1. Save the LISP code in a .lsp file (e.g., DivideArea.lsp).
  2. Open AutoCAD.
  3. Use the APPLOAD command to load the .lsp file.
  4. You can also load the file by dragging and dropping it into the AutoCAD workspace.

4. Run the Routine

  1. Type DivideArea in the command line and press Enter.
  2. Select the closed polyline representing the area.
  3. Enter the number of divisions when prompted.
  4. The routine will create dividing lines within the area.

5. Adapt for Custom Needs

If your area isn’t rectangular or needs a specific division strategy (e.g., horizontal lines or irregular splits), modify the script:

  • Use vlax-curve-getarea or advanced methods to calculate the area for irregular shapes.
  • Adjust the logic to suit the desired layout.

Notes

  • Ensure the area is a closed polyline before running the routine.
  • For complex or non-rectangular areas, you may need advanced logic or manual adjustments.
  • Test the script on a sample file to validate its functionality.

Would you like to modify this example to suit a specific requirement?













































Comments

Popular posts from this blog

Calculating Areas of Multiple Objects in AutoCAD

Coordinate Lisp in AutoCAD

AutoCAD Lisp to Print Multiple Sheets at Once (TPL)