Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations SDETERS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

A Sap2000 API question, SapObject.SapModel.SolidObj.AddByCoord()

GJoeRMK

Structural
Aug 19, 2024
18
Hello fellow engineers,
I am working on a simple script to create a slab from 3D elements, here is my code:

Python:
import comtypes.client as cc
import numpy as np
import os
# Intenta conectar a una instancia existente de SAP2000 usando GetActiveObject
SapObject = cc.GetActiveObject("CSI.SAP2000.API.SapObject")
# Obtiene el objeto del modelo
SapModel = SapObject.SapModel
units = 8
#ret = SapModel.InitializeNewModel(units)
ret = SapModel.SetPresentUnits(units)
# ret = SapModel.File.NewBlank()
nombre_material = "Concrete210"
ret = SapModel.PropMaterial.SetMaterial(nombre_material, 2)  # 2 para concreto
print(f"Estado de creación/modificación del material: {ret}")
E_concreto = 2.1e10
nu_concreto = 0.2
peso_volumetrico = 23500.0
coeficiente_termico = 5.5e-6  # Agregado coeficiente térmico.  Valor de ejemplo.
ret = SapModel.PropMaterial.SetMPIsotropic(nombre_material, E_concreto, nu_concreto, coeficiente_termico)
if ret != 0:
    print("Error al asignar propiedades isotrópicas del material")
nombre_prop_solido = "SolidoConcreto210"
a=0
b=0
c=0
Incompatible = True
ret = SapModel.PropSolid.SetProp(nombre_prop_solido, nombre_material, 0.0, 0.0, 0.0, True)
print(f"Estado de creación de la propiedad de sólido: {ret}")
ret = SapModel.PropSolid.SetProp("Alternativo2", "4000Psi", 0.0, 0.0, 0.0, True)
NumberNames = 1
MyName = ""
ret = SapModel.PropMaterial.GetNameList(NumberNames, MyName)
print(f"Lista de materiales creados {ret}")
ret = SapModel.SolidElm.GetNameList(NumberNames, MyName)
print(f"Lista de sólidos elementos creados {ret}")
ret = SapModel.SolidObj.GetNameList(NumberNames, MyName)
print(f"Lista de sólidos objetos creados {ret}")

ret = SapModel.PropSolid.GetProp(nombre_prop_solido)
print(f"Propiedades del sólido: {ret}")
'''
# Solo usar para crear un nuevo modelo
size = 8
ret = SapModel.File.NewSolidBlock(size, size, size,"" ,"Default", size, size, size)
#ret = SapModel.SolidObj.SetProperty("1", nombre_prop_solido)
print(f"Estado de creación del sólido: {ret}")
'''
X = [0, 2, 2, 0, 0, 2, 2, 0]
Y = [0, 0, 2, 2, 0, 0, 2, 2]
Z = [0, 0, 0, 0, 2, 2, 2, 2]
print(type(X[0]))
print(type(X))
resultado = SapModel.SolidObj.AddByCoord(X, Y, Z, "SolidoRevolucion" , nombre_prop_solido)
print("Estado de creación del sólido: ", resultado)
ret = SapModel.View.RefreshView(0, False)
ret = SapModel.SolidObj.Count()
print(f"Número total de sólidos en el modelo: {ret}")
ret = SapModel.SolidObj.Delete("SolidoRevolucion")
print(f"Estado de eliminación del sólido: {ret}")

'''
# 3) Crear 8 puntos en coordenadas definidas (por ejemplo, un cubo de 2×2×2)
pointNames = []
coords = [
    (0,0,0),   (2,0,0),   (0,2,0),   (2,2,0),
    (0,0,2),   (2,0,2),   (0,2,2),   (2,2,2)
]
for i, (x, y, z) in enumerate(coords):
    pName = ""
    # AddCartesian(x, y, z, [CSys], [NameUser], [UserSystem])
    # Retorna (Name, ret_code)
    name_created, ret = SapModel.PointObj.AddCartesian(x, y, z, "", "")
    if ret != 0:
        print(f"Error al crear punto en coords=({x},{y},{z}). Código={ret}")
    pointNames.append(name_created)

print(f"Lista de puntos creados: {pointNames}")
#Creacion del solido a partir de los puntos
result = SapModel.SolidObj.AddByPoint(pointNames, "NamePlaceholder", nombre_prop_solido, "")
'''
number_points = 6
# Creacion de un elemento area
X = [5, 10, 15, 10, 5, 0]       # en el doc, x(0)=50, x(1)=100, ...
Y = [0,   0,   4,  8, 8, 4]      # y(0)=0,   y(1)=0,   ...
Z = [0,   0,   0,   0,  0,  0]       # asume z=0 para un área en el plano XY
name_area_obj = "AreaName"
# AddByCoord(NumberPoints, x(), y(), z(), Name, [PropName="Default"], [UserName=""], [CSys="Global"])
result = SapModel.AreaObj.AddByCoord(number_points, X, Y, Z, name_area_obj )
print("Estado de creación del area: ", result)
ret = SapModel.View.RefreshView(0, False)

# Directorio y archivo para guardar el modelo
APIPath = r"D:\DocLibrary\Tesis de Titulacion\Tesis2024\scripts"
if not os.path.exists(APIPath):
    try:
        os.makedirs(APIPath)
    except OSError:
        pass
ModelPath = os.path.join(APIPath, 'API_solid_ex1.sdb')
# Guardar modelo
ret = SapModel.File.Save(ModelPath)
if ret != 0:
    print(f"Error al guardar el archivo. Código de error: {ret}")

The most important part is here:
Python:
X = [0, 2, 2, 0, 0, 2, 2, 0]
Y = [0, 0, 2, 2, 0, 0, 2, 2]
Z = [0, 0, 0, 0, 2, 2, 2, 2]
result = SapModel.SolidObj.AddByCoord(X, Y, Z, "SolidoRevolucion" , nombre_prop_solido)
print("Estado de creación del sólido: ", resultado)


ret = SapModel.SolidObj.Count()
print(f"Número total de sólidos en el modelo: {ret}")


After that, the solid was not created besides returning 0, so that makes question where it go wrong?
 
Replies continue below

Recommended for you

Just for curiosity I change the order on how I defined the points:
Before
X = [0, 2, 2, 0, 0, 2, 2, 0]
Y = [0, 0, 2, 2, 0, 0, 2, 2]
Z = [0, 0, 0, 0, 2, 2, 2, 2]

After
X = [0, 2, 0, 2, 0, 2, 0, 2]
Y = [0, 0, 2, 2, 0, 0, 2, 2]
Z = [0, 0, 0, 0, 2, 2, 2, 2]


That minor difference was key in creating a solid object, it was not state in the API. But looking at CSI analysis reference.pdf I found an interesting picture
1743622401622.png
Look at that!
 
Look at this slab, beatiful!

Code:
X_losa = [0.0, 3.0, 0.0, 3.0, 0.0, 3.0, 0.0, 3.0]
Y_losa = [0.0, 0.0, 6.0, 6.0,  0.0, 0.0, 6.0, 6.0]
Z_losa = [2.4, 2.4, 2.4, 2.4, 2.55, 2.55, 2.55, 2.55]
ret_losa = SapModel.SolidObj.AddByCoord(X_losa, Y_losa, Z_losa, "Losa", nombre_prop_solido)

z_top_vig = 2.4
z_bot_vig = 2.25
ancho_vigueta = 0.05
separacion_viguetas = 0.40
largo_x = 3.0
largo_y = 6.0

x_actual = 0.0
contador_vigas = 0
while x_actual < largo_x:
    x1 = x_actual
    x2 = x_actual + ancho_vigueta
    if x2 > largo_x:
        break

    # Un bloque de vigueta: en Y=0..6, Z=-0.05..-0.20
    # Ocho vértices
    X_vig = [x1, x2, x1, x2, x1, x2, x1, x2]
    Y_vig = [0.0, 0.0, largo_y, largo_y,  0.0, 0.0, largo_y, largo_y]
    Z_vig = [
        z_bot_vig, z_bot_vig, z_bot_vig, z_bot_vig,
        z_top_vig, z_top_vig, z_top_vig, z_top_vig
    ]
    nombre_vig = f"Viga_{int(x_actual*100)}"
    ret_viga = SapModel.SolidObj.AddByCoord(X_vig, Y_vig, Z_vig, nombre_vig, nombre_prop_solido)

    x_actual += separacion_viguetas
1743645955581.png
 

Part and Inventory Search

Sponsor