Python Code to extrude a circle to a separate circle that is offset of the first circle.

Python Code to extrude a circle to a separate circle that is offset of the first circle.

sjelen
Contributor Contributor
732 Views
24 Replies
Message 1 of 25

Python Code to extrude a circle to a separate circle that is offset of the first circle.

sjelen
Contributor
Contributor

Good morning, everyone!

 

Is there a way to extrude or loft 1 circle to another circle to create a solid cylinder where the circles are drawn on the xz plane but are not directly inline with one another. Circle 1 lets say is at 10, 0 , 0 and Circle 2 is at 15, 20, 0. 

 

Does pyautocad have a function that will perform this extrusion? 

 

I have thought about maybe creating a point in the center of the circles and then creating a line between the two circles as a rail for the extrusion to follow. Would I even need to go that far? 

 

Thank you all for your time!

0 Likes
733 Views
24 Replies
Replies (24)
Message 21 of 25

sjelen
Contributor
Contributor
(defun tube (ent rad / obj start end len)
(setq obj (vlax-ename->vla-object ent))
(setq start (vlax-curve-getstartPoint obj))
(setq end (vlax-curve-getEndPoint obj))
(setq len (vlax-get obj 'length))
(command "UCS" "X" 90)
(command "circle" start rad)
(setq ent2 (entlast))
(command "rotate3d" ent2 "" "Y" "0,0,0" (/ pi 2.))
(command "extrude" ent2 "" len)
;(command "UCS" "W")
(princ)
)
 
I thought I could do something like this but got this outcome. 
sjelen_1-1749664796427.png

 

0 Likes
Message 22 of 25

sjelen
Contributor
Contributor
(defun tube (ent rad / obj start end len)
(setq obj (vlax-ename->vla-object ent))
(setq start (vlax-curve-getstartPoint obj))
(setq end (vlax-curve-getEndPoint obj))
(setq len (vlax-get obj 'length))
(command "UCS" "X" 90)
(command "circle" start rad)
(setq ent2 (entlast))
(command "rotate3d" ent2 "" "Y" "0,0,0" (/ pi 2.))
(command "extrude" ent2 "" len)
(command "UCS" "W")
(princ)
)
 
and like this if I uncomment the World view
sjelen_2-1749664974068.png

 

0 Likes
Message 23 of 25

sjelen
Contributor
Contributor

Dang, apologies for all the responses. I took a step back and realized my issue was within the CSV file. I made those changes and success. So I think I should be able to reconfigure the CSV with additional points and make a second set of cylinders perpendicular to these I believe...😬

sjelen_0-1749665946723.png

 

 

0 Likes
Message 24 of 25

sjelen
Contributor
Contributor

This is so awesome! Really excited about this. Apologies again for the numerous posts today. But I got it to work and added a separate if statement. Wanting to double check that it's correct. It did work as I planned it to but possible is out of order? The point of the if statement is for points that would be sleeves I would want the cylinder to be extruded along the Z axis. It worked great when there was 1 point but as soon as I added another separate point the code read it as the "pair" and then extruded over to it instead of down so wondering how I can fix that. 

 

; drawtubes by AlanH June 2025
; reads two xyz points and radius in a csv file

; thanks to Lee-mac for this defun
(defun C:dotubes ( / oldsnap oldaunits csv fo str x1 x2 y1 y2 z1 z2 rad)
(defun csv->lst (str del / pos )
(if (setq pos (vl-string-position del str))
    (cons (substr str 1 pos) (csv->lst (substr str (+ pos 2)) del))
    (list str)
    )
)

(defun tube (ent rad / obj start end len)
(setq obj (vlax-ename->vla-object ent))
(setq start (vlax-curve-getstartPoint obj))
(setq end (vlax-curve-getEndPoint obj))
(setq len (vlax-get obj 'length))
(command "UCS" "3" start end "")
(command "circle" "0,0,0" rad)
(setq ent2 (entlast))
(command "rotate3d" ent2 "" "Y" "0,0,0" (/ pi 2.))
(command "extrude" ent2 "" len)
(command "UCS" "W")
(princ)
)

;;starts here

(setq oldsnap (getvar 'osmode))
(setvar 'osmode 0)
(setq oldaunits (getvar 'aunits))
(setvar 'aunits 3)

(setq csv (getfiled "Select CSV File" "" "csv" 16))
(setq fo (open csv "R"))
(setq str (read-line fo))

(while (setq str (read-line fo))
  (setq lst (csv->lst str 44))
  (setq x1 (atof (nth 1 lst))
        y1 (atof (nth 2 lst))  
        z1 (atof (nth 3 lst))
        rad (atof (nth 4 lst)))

  ;;Read the next line
  (setq str (read-line fo))

  (if (or (null str) (= str ""))
    ;; If only one point provided — draw circle and extrude along Z
    (progn
      (command "UCS" "W")
      (command "circle" (list x1 y1 z1) rad)
      (setq ent2 (entlast))
      (command "extrude" ent2 "" 10.0) ;; Extrude along Z axis by 10 units
    )
    ;; Two points provided — draw line and tube
    (progn
      (setq lst (csv->lst str 44))
      (setq x2 (atof (nth 1 lst))
            y2 (atof (nth 2 lst))
            z2 (atof (nth 3 lst)))
      (command "line" (list x1 y1 z1)(list x2 y2 z2) "")
      (tube (entlast) rad)
    )
  )
)


(CLOSE FO)
(command "zoom" "e")
(setvar 'aunits oldaunits)
(setvar 'osmode oldsnap)
(princ)
)
(c:dotubes)
 
sjelen_1-1749670017867.pngsjelen_2-1749670050083.png

 

 
0 Likes
Message 25 of 25

Sea-Haven
Mentor
Mentor

Sleeves along the Z axis, should be achievable by just using correct XYZ values. Where XY are same but Z is different should do a vertical tube. I did find need new code for a true vertical Z can use (if x1=x2 y1=y2 and so on, then don't use a new ucs remain in world. Used 0,0,0 & 1,1,30 for testing and appears ok. 0,0,0 0,0,30 will draw in wrong direction using current code.

 

0 Likes