Multiple Models#
|
This example demonstrates how to interact with multiple RSTAB models within the same script:
|
from dlubal.api import rstab, common
# -------------------------------------------------------
# This example demonstrates how to interact with multiple RSTAB models
# using the RSTAB API. The script includes creating, updating,
# and querying model object/s in two separate models in one script.
# -------------------------------------------------------
# Function to create, update, and query model object/s
def create_update_and_query_objects(rstab_app, model_id=None):
# Create a list of objects
rstab_app.create_object_list(
objs=[
rstab.structure_core.Node(no=1, coordinate_1=0.0),
rstab.structure_core.Node(no=2, coordinate_1=2.0),
rstab.structure_core.Node(no=3, coordinate_1=3.0),
rstab.structure_core.Material(no=1, name='S235'),
rstab.structure_core.CrossSection(no=1, name='IPE 200', material=1)
],
model_id=model_id
)
# Create a single object
rstab_app.create_object(
obj=rstab.structure_core.Member(no=1, cross_section_start=1, node_start=1, node_end=3),
model_id=model_id
)
# Update a list of objects
rstab_app.update_object_list(
objs=[
rstab.structure_core.Node(no=1, coordinate_2=1.0),
rstab.structure_core.Node(no=2, coordinate_2=1.0),
rstab.structure_core.Node(no=3, coordinate_2=1.0),
],
model_id=model_id
)
# Update a single object
rstab_app.update_object(
obj=rstab.structure_core.Node(
no=2,
type=rstab.structure_core.Node.TYPE_ON_MEMBER,
on_member_reference_member=1,
distance_from_start_absolute=2.0
),
model_id=model_id
)
# Retrieve a list of objects
node_list = rstab_app.get_object_list(
objs=[rstab.structure_core.Node()],
model_id=model_id
)
for node in node_list:
print(f"{node.DESCRIPTOR.name}: {node.no} [X={node.coordinate_1}, Y={node.coordinate_2}] | Type: {node.type}")
# Retrieve a single objects
member = rstab_app.get_object(
obj=rstab.structure_core.Member(no=1)
)
print(f"{member.DESCRIPTOR.name}: {member.no}, Length: {member.length} [m]")
# --- MAIN SCRIPT ---
# Connect to the RSTAB application
with rstab.Application() as rstab_app:
rstab_app.close_all_models(save_changes=False)
# Create two separate models and store their model IDs
model_1: common.ModelId = rstab_app.create_model(name="model_1")
model_2: common.ModelId = rstab_app.create_model(name="model_2")
model_list = rstab_app.get_model_list()
print(f"\nModel list:\n {model_list}")
# Make operations on currently active model (default is model_2)
active_model = rstab_app.get_active_model()
print(f"\nCurrently active model_2:\n {active_model}")
create_update_and_query_objects(rstab_app) # Use default active model
# Perform operations on model_1 by passing its model ID
print(f"\nSwitching to model_1 (by ID):\n {model_1}")
create_update_and_query_objects(rstab_app, model_id=model_1)
# Set model_1 as the active model
rstab_app.set_active_model(model_id=model_1)
active_model = rstab_app.get_active_model()
print(f"\nSetting model_1 as active:\n {active_model}")
using Rstab = Dlubal.Api.Rstab;
using Common = Dlubal.Api.Common;
using Google.Protobuf;
using System.Threading.Tasks;
// -------------------------------------------------------
// This example demonstrates how to interact with multiple RSTAB models
// using the RFEM API. The code includes creating, updating,
// and querying model object/s in two separate models within one script.
// -------------------------------------------------------
// Function to create, update, and query model object/s
static async Task CreateUpdateAndGetObjects(ApplicationRstab rstabApp, Common.ModelId? modelId = null)
{
// Create a list of objects
await rstabApp.create_object_list(
objs: new List<IMessage> {
new Rstab.StructureCore.Node{No=1, Coordinate1=0.0},
new Rstab.StructureCore.Node{No=2, Coordinate1=2.0},
new Rstab.StructureCore.Node{No=3, Coordinate1=3.0},
new Rstab.StructureCore.Material{No=1, Name="S235"},
new Rstab.StructureCore.CrossSection{No=1, Name="IPE 200", Material=1},
},
modelId: modelId
);
// Create a single object
await rstabApp.create_object(
obj: new Rstab.StructureCore.Member{No=1, CrossSectionStart=1, NodeStart=1, NodeEnd=3},
modelId: modelId
);
// Update a list of objects
await rstabApp.update_object_list(
objs: new List<IMessage> {
new Rstab.StructureCore.Node{No=1, Coordinate2=1.0},
new Rstab.StructureCore.Node{No=2, Coordinate2=1.0},
new Rstab.StructureCore.Node{No=3, Coordinate2=1.0},
},
modelId: modelId
);
// Update a single object
await rstabApp.update_object(
obj: new Rstab.StructureCore.Node{
No=2,
Type=Rstab.StructureCore.Node.Types.Type.OnMember,
OnMemberReferenceMember=1,
DistanceFromStartAbsolute=2.0
},
modelId: modelId
);
// Retrieve a list of objects
var nodeList = await rstabApp.get_object_list(
objs: new List<IMessage> {
new Rstab.StructureCore.Node{}
},
modelId: modelId
);
foreach (var obj in nodeList)
{
if (obj is Rstab.StructureCore.Node node)
{
Console.WriteLine($"{Rstab.StructureCore.Node.Descriptor.Name}: {node.No} [X={node.Coordinate1}, Y={node.Coordinate2}] | Type: {node.Type}");
}
}
// Retrieve a single object
var line = await rstabApp.get_object<Rstab.StructureCore.Member>(
obj: new Rstab.StructureCore.Member{ No = 1 }
);
Console.WriteLine($"{Rstab.StructureCore.Member.Descriptor.Name}: {line.No}, Length: {line.Length} [m]");
}
ApplicationRstab? rstabApp = null;
try
{
rstabApp = new ApplicationRstab();
await rstabApp.close_all_models(saveChanges: false);
// Create two separate models and store their model IDs
Common.ModelId model_1 = await rstabApp.create_model(name: "model_1");
Common.ModelId model_2 = await rstabApp.create_model(name: "model_2");
var modelList = await rstabApp.get_model_list();
Console.WriteLine($"\nModel list:\n{modelList}");
// Make operations on current active model (=model_2)
var activeModel = await rstabApp.get_active_model();
Console.WriteLine($"\nCurrently active model_2:\n{activeModel}");
await CreateUpdateAndGetObjects(rstabApp, null);
// Make operations on next model using its ID (=model_1)
Console.WriteLine($"\nSwitching to model_1 (by ID):\n{model_1}");
await CreateUpdateAndGetObjects(rstabApp, model_1);
// Set as active model this model (=model_1)
await rstabApp.set_active_model(modelId: model_1);
var newActiveModel = await rstabApp.get_active_model();
Console.WriteLine($"\nSetting as active model_1\n{newActiveModel}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
finally
{
if (rstabApp != null) await rstabApp.close_connection();
}