How to create a coordinate system based on direction vectors and a specific origin?

Rohith Patchigolla
Rohith Patchigolla Member, Moderator, Employee Posts: 186
100 Comments 25 Answers Second Anniversary 25 Likes
✭✭✭✭

I have principal axis vectors and center of gravity of a body. Now I want to create a coordinate system based on these vectors and center. But, Mechanical doesn't let me input vector information.

Answers

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 186
    100 Comments 25 Answers Second Anniversary 25 Likes
    ✭✭✭✭
    edited December 2023

    This script is tested in 2022R2. Simply open Mechanical --> go to Automation Tab --> hit Scripting button and paste the below script in Script Editor -> Run script

    #Inputs
    #Center of Gravity
    Xcog = Quantity(100,'mm')
    Ycog = Quantity(200,'mm')
    Zcog = Quantity(300,'mm')
    
    #Principal Axis Vectors
    Xaxis = [0.99997, -0.00777, -0.00092]
    Yaxis = [0.00772, 0.99886, -0.04717]
    #Zaxis = [0.00129, 0.04716, 0.99889]
    
    def create_beams_from_points(reinforcements_point_list):
     with Transaction(True):
        ExtAPI.DataModel.Project.Model.AddConstructionGeometry()
        construction_line=ExtAPI.DataModel.Project.Model.ConstructionGeometry.AddConstructionLine()
        for pointlist in reinforcements_point_list:
            points=construction_line.CreatePoints(pointlist)
            for i in range(0,len(points)-1):
              edge = construction_line.CreateStraightLines([points[i],points[i+1]], [(0,1)])
        part_geo = construction_line.AddToGeometry()
        part = ExtAPI.DataModel.Project.Model.Geometry.GetPart(part_geo)
        geo_bodies= part_geo.Bodies
        treebodies=map(lambda x: ExtAPI.DataModel.Project.Model.Geometry.GetBody(x), geo_bodies)
    
    points1 =[[(Xcog.Value,Ycog.Value,Zcog.Value), (Xcog.Value+Xaxis[0], Ycog.Value+Xaxis[1],Zcog.Value+Xaxis[2])]]
    create_beams_from_points(points1)
    
    points2 =[[(Xcog.Value,Ycog.Value,Zcog.Value), (Xcog.Value+Yaxis[0], Ycog.Value+Yaxis[1],Zcog.Value+Yaxis[2])]]
    create_beams_from_points(points2)
    
    constructionLines = list(DataModel.GetObjectsByType(DataModelObjectCategory.ConstructionLine))
    lineX = constructionLines[-2]
    lineY = constructionLines[-1]
    
    beamX_Id = lineX.GetPartFromGeometry().Bodies[0].Edges[0].Id
    beamY_Id = lineY.GetPartFromGeometry().Bodies[0].Edges[0].Id
    
    with Transaction(True):
        #Add Coordinate System
        myCsys = Model.CoordinateSystems.AddCoordinateSystem()
        myCsys.OriginDefineBy = CoordinateSystemAlignmentType.Fixed
        myCsys.OriginX = Xcog
        myCsys.OriginY = Ycog
        myCsys.OriginZ = Zcog
    
        myCsys.PrimaryAxis = CoordinateSystemAxisType.PositiveXAxis
        myCsys.SecondaryAxis = CoordinateSystemAxisType.PositiveYAxis
        myCsys.PrimaryAxisDefineBy = CoordinateSystemAlignmentType.Associative
        myCsys.SecondaryAxisDefineBy = CoordinateSystemAlignmentType.Associative
    
        beamX_Sel = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
        beamX_Sel.Ids = [beamX_Id]
    
        beamY_Sel = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
        beamY_Sel.Ids = [beamY_Id]
    
        myCsys.PrimaryAxisLocation = beamX_Sel
        myCsys.SecondaryAxisLocation = beamY_Sel
    
        myCsys.PrimaryAxisDefineBy = CoordinateSystemAlignmentType.Fixed
        myCsys.SecondaryAxisDefineBy = CoordinateSystemAlignmentType.Fixed
    
        lineX.RemoveFromGeometry()
        lineY.RemoveFromGeometry()
    
        DataModel.Remove(lineX)
        DataModel.Remove(lineY)
    

    Created Coordiante System

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee Posts: 295
    25 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    You might also be interested in some of the functions in the attached file, such as RotCS(cs,Axes).