Creating 3D AutoCAD objects in Python

Below Python code creates some 3D AutoCAD objects using the pyautocad in Python.

import math
import pyautocad

# AutoCAD instance
acad = pyautocad.Autocad()

# active user coordinate system is WCS
acad.ActiveDocument.UserCoordinateSystem = acad.GetInterfaceObject("AutoCAD.AcCmCoordSystem").WorldCoordinateSystem

# pyramid object in 3D
p1 = (0, 0, 0)
p2 = (5, 0, 0)
p3 = (5, 5, 0)
p4 = (0, 5, 0)
p5 = (2.5, 2.5, 5)
pyramid = acad.model.AddPyramid(p1, p2, p3, p4, p5)

# sphere
center = (10, 10, 0)
radius = 5
sphere = acad.model.AddSphere(center, radius)

# cylinder
center = (20, 20, 0)
radius = 3
height = 10
cylinder = acad.model.AddCylinder(center, radius, height)

# cone
center = (30, 30, 0)
radius1 = 5
radius2 = 3
height = 8
cone = acad.model.AddCone(center, radius1, radius2, height)

# torus
center = (40, 40, 0)
major_radius = 5
minor_radius = 2
torus = acad.model.AddTorus(center, major_radius, minor_radius)

# set viewport to 3D mode
acad.ActiveDocument.ActiveViewport = acad.GetInterfaceObject("AutoCAD.AcDbViewport")
acad.ActiveDocument.ActiveViewport.ViewMode = acad.GetInterfaceObject("AutoCAD.AcDbAbstractViewTableRecord.ViewModeType").k3DWireframe

# zoom to the extents of the drawing
acad.ZoomExtents()

In above Python code I use the methods AddTorus(), AddCone(), AddCylinder(), and AddSphere(), AddPyramid() to create some exemplaric 3D AutoCAD objects in the drawing.