Skip to content

Commit

Permalink
Merge pull request #25 from arup-group/Task/ADSEC-2019-create-sample-…
Browse files Browse the repository at this point in the history
…files-for-using-the-API-with-Excel

ADSEC-2019 create sample files for using the api with excel
  • Loading branch information
AnishSanghai123 authored Jun 7, 2024
2 parents 8fc09a3 + c5df664 commit 9a93836
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
94 changes: 94 additions & 0 deletions AdSec/.NET/ApiToExcel/ApiToExcel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using ClosedXML.Excel; //Open source library That can be used for reading and writing in the excel file
using System;
using System.IO;
using System.Linq;
using Oasys.AdSec;
using OasysUnits;
using OasysUnits.Units;
using Oasys.Profiles;
using Oasys.AdSec.StandardMaterials;
using Oasys.AdSec.DesignCode;
using Oasys.AdSec.Materials;

class Program
{
static void Main()
{
// Part1
// Getting the workbook from the excel file.
var excelfilePath = CreateExcelWithRandomLoads();

// Part2
// Open a workbook with worksheet with name "Sheet1"
var workbook = new XLWorkbook(excelfilePath);
var worksheet = workbook.Worksheets.Worksheet("Sheet1");

// creating adsec object and defining section material
var sectionMaterial = Steel.EN1993.Edition_2005.S235;
var adSec = IAdSec.Create(EN1992.Part1_1.Edition_2004.NationalAnnex.GB.Edition_2014);

var section = CreateSection(sectionMaterial);

//Analysing the section
var circleSectionAnalysis = adSec.Analyse(section);

for (int i = 1; i <= 10; i++)
{
var circleLoad = CreateCircleLoad(worksheet, i);

//Getting the strength results
var strengthResult = circleSectionAnalysis.Strength.Check(circleLoad);
var utilisation = Math.Round(strengthResult.LoadUtilisation.Percent, 1);
var ulsStatus = utilisation < 100 ? "Clear" : "Not Clear";
worksheet.Cell("D" + i).Value = utilisation;
worksheet.Cell("E" + i).Value = ulsStatus;
}
// excel file will be created in the temp folder
workbook.Save();
}

private static string CreateExcelWithRandomLoads()
{
var workbook = new XLWorkbook();
var worksheet = workbook.AddWorksheet("Sheet1");

var random = new Random();

// adding random values into cells
for (int i = 1; i <= 10; i++)
{
worksheet.Cell(i, 1).Value = random.Next(-100, 101) * 1000;
worksheet.Cell(i, 2).Value = random.Next(-100, 101) * 1000;
worksheet.Cell(i, 3).Value = random.Next(-100, 101) * 1000;
}

var filePath = Path.Combine(Path.GetTempPath(), "Output.xlsx");
workbook.SaveAs(filePath);

return filePath;
}

private static ILoad CreateCircleLoad(IXLWorksheet worksheet, int i)
{
// Getting the loads and coverting it
var force_x = new Force((double)worksheet.Cell("A" + i).Value, ForceUnit.Kilonewton);
var moment_yy = new Moment(
(double)worksheet.Cell("B" + i).Value,
MomentUnit.KilonewtonMeter
);
var moment_zz = new Moment(
(double)worksheet.Cell("C" + i).Value,
MomentUnit.KilonewtonMeter
);
var circleLoad = ILoad.Create(force_x, moment_yy, moment_zz);
return circleLoad;
}

private static ISection CreateSection(ISteel sectionMaterial)
{
var diameter = new Length(80, LengthUnit.Inch);
var circleProfile = ICircleProfile.Create(diameter);
var section = ISection.Create(circleProfile, sectionMaterial);
return section;
}
}
10 changes: 10 additions & 0 deletions AdSec/.NET/ApiToExcel/ApiToExcel.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
90 changes: 90 additions & 0 deletions AdSec/Python/ApiToExcel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Load the AdSec API
import os
import tempfile
from random import randint

# importing packages
import openpyxl as op # OPen source library that can be used for reading and writing into the excel files
from Oasys.AdSec import IAdSec, ILoad, ISection
from Oasys.AdSec.DesignCode import EN1992
from Oasys.AdSec.StandardMaterials import Steel
from Oasys.Profiles import ICircleProfile
from OasysUnits import Force, Length, Moment
from OasysUnits.Units import ForceUnit, LengthUnit, MomentUnit


def main():
# Part1
# Getting the workbook from the excel file.
excelfile_path = create_excel_with_random_loads()

# Part2
# Open a workbook with worksheet with name "Sheet1"
workbook = op.load_workbook(excelfile_path)
worksheet = workbook["Sheet1"]

# creating adsec object and defining section material
section_material = Steel.EN1993.Edition_2005.S235
ad_sec = IAdSec.Create(EN1992.Part1_1.Edition_2004.NationalAnnex.GB.Edition_2014)

section = create_section(section_material)

# Analysing the section
circle_section_analysis = ad_sec.Analyse(section)

max_utilisation = 100

for i in range(1, 11):
circle_load = create_circle_load(worksheet, i)

# Getting the strength results
strength_result = circle_section_analysis.Strength.Check(circle_load)
utilisation = round(strength_result.LoadUtilisation.Percent, 1)
uls_status = "Clear" if utilisation < max_utilisation else "Not Clear"
worksheet[f"D{i}"].value = utilisation
worksheet[f"E{i}"].value = uls_status

# excel file will be created in the temp folder
workbook.save(excelfile_path)


def create_excel_with_random_loads():
temp_fd, temp_path = tempfile.mkstemp(prefix="OUTPUT", suffix=".xlsx")
os.close(temp_fd)
workbook = op.Workbook()
worksheet = workbook.active
worksheet.title = "Sheet1"

# adding random values into cells
for i in range(1, 11):
worksheet.cell(i, 1, value=randint(-100, 100) * 1000)
worksheet.cell(i, 2, value=randint(-100, 100) * 1000)
worksheet.cell(i, 3, value=randint(-100, 100) * 1000)

workbook.save(temp_path)

return temp_path


def create_circle_load(worksheet, i):
# Getting the loads and converting it
force_x = worksheet[f"A{i}"].value
moment_yy = worksheet[f"B{i}"].value
moment_zz = worksheet[f"C{i}"].value
circle_load = ILoad.Create(
Force(force_x, ForceUnit.Kilonewton),
Moment(moment_yy, MomentUnit.KilonewtonMeter),
Moment(moment_zz, MomentUnit.KilonewtonMeter),
)
return circle_load


def create_section(section_material):
diameter = Length(80, LengthUnit.Inch)
circle_profile = ICircleProfile.Create(diameter)
section = ISection.Create(circle_profile, section_material)
return section


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions AdSec/Python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
oasys.adsec
svglib==1.3.0
pathlib
openpyxl==3.1.2

0 comments on commit 9a93836

Please sign in to comment.