How to capture the model mass information?

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

After extracting the mid-surface in SpaceClaim, has multiple “Face Thickness” under one component (as shown below figure). When importing into Mechanical, the mass information cannot be read, but calculations can still be performed normally. If the model has one “Face Thickness” per component, the mass information would be displayed normally.

How can the mass information be captured for below model?

Tagged:

Best Answer

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 206
    100 Comments 25 Answers Second Anniversary 25 Likes
    ✭✭✭✭
    Answer ✓

    I tried to extract this using Python Code object with "After Object Changed" callback at Model level (RMB on Model --> Insert --> Python Code).

    Please the below scripts for "Script" tab and "Property Provider" tab.

    Script part:

    def after_object_changed(this, object_changed, property_name):# Do not edit this line
        import units
        import materials 
    
        geometry_unit = ExtAPI.DataModel.Project.Model.Analyses[0].GeoData.Unit
        ActiveMechanicalUnitSystem = ExtAPI.Application.ActiveUnitSystem
        if ActiveMechanicalUnitSystem.ToString() == "StandardMKS":
            convertTo_unit = 'DS_SOLV_MKS'
        elif ActiveMechanicalUnitSystem.ToString() in ["StandardNMMton", "StandardNMM", "StandardNMMdat"]:
            convertTo_unit = 'DS_SOLV_NMM'
        elif ActiveMechanicalUnitSystem.ToString() == "StandardCGS":
            convertTo_unit = 'DS_SOLV_CGS'
        elif ActiveMechanicalUnitSystem.ToString() == "StandardUMKS":
            convertTo_unit = 'DS_SOLV_UMKS'
    
        allBodies = [child for child in DataModel.GetObjectsByType(DataModelObjectCategory.Body) if child.ObjectState.ToString() != "Suppressed"]
        total_mass = 0
        total_volume = 0
        for body in allBodies:
            bodyMass = body.Mass.Value
            bodyVolume = body.Volume.Value
            total_mass = total_mass + bodyMass
            total_volume = total_volume + bodyVolume
    
        allFaceThicknessObj = [child for child in DataModel.GetObjectsByType(DataModelObjectCategory.Thickness) if child.ObjectState.ToString() != "Suppressed"]
    
        substract_volume_total = 0
        volume_faceThickness_total = 0
        mass_faceThickness_total = 0
        mass_substract_total = 0
    
        for obj in allFaceThicknessObj:
            substract_volume = 0
            volume_faceThickness = 0
            mass_faceThickness = 0
            mass_substract = 0
            thicknessData = obj.ThicknessField.Output.GetDiscreteValue(0).Value
            faceIdsScoped = obj.Location.Ids
            for faceId in faceIdsScoped:
                face = DataModel.GeoData.GeoEntityById(faceId)
                faceArea = Quantity(face.Area,'m m')
                faceAreaActiveUnit = faceArea.ConvertToUnitSystem(convertTo_unit, "Area")
                volume_faceThickness = volume_faceThickness + faceAreaActiveUnit.Value*thicknessData
                faceBody = face.Bodies[0]
                treebody=ExtAPI.DataModel.Project.Model.Geometry.GetBody(faceBody)
                if treebody.ObjectState.ToString() != "Suppressed":
                    faceBodyThickness = Quantity(faceBody.Thickness, 'm')
                else:
                    faceBodyThickness = Quantity(0, 'm')
                faceBodyThicknessActiveUnit = faceBodyThickness.ConvertToUnitSystem(convertTo_unit, "Length")
                substract_volume = substract_volume + faceAreaActiveUnit.Value*faceBodyThicknessActiveUnit.Value
                treebody=ExtAPI.DataModel.Project.Model.Geometry.GetBody(faceBody)
                bodyMat = treebody.Material
                myMat = DataModel.GetObjectsByName(bodyMat)[0]
                engineeringData = myMat.GetEngineeringDataMaterial()
                myDens = materials.GetMaterialPropertyByName(engineeringData,'Density')
                DensUnit = myDens['Density'][0]
                DensVal = myDens['Density'][1]
                if treebody.ObjectState.ToString() != "Suppressed":
                    myDens = Quantity(DensVal,DensUnit)
                else:
                    myDens = Quantity(0,DensUnit)
                myDens_ActiveUnit = myDens.ConvertToUnitSystem(convertTo_unit, "Density")
                mass_faceThickness = mass_faceThickness + myDens_ActiveUnit.Value*volume_faceThickness
                mass_substract = mass_substract + myDens_ActiveUnit.Value*faceAreaActiveUnit.Value*faceBodyThicknessActiveUnit.Value
            substract_volume_total = substract_volume_total + substract_volume
            volume_faceThickness_total = volume_faceThickness_total + volume_faceThickness
            mass_faceThickness_total = mass_faceThickness_total + mass_faceThickness
            mass_substract_total = mass_substract_total + mass_substract
        Total_Volume_New = total_volume + volume_faceThickness_total - substract_volume_total
        Total_Mass_New = total_mass + mass_faceThickness_total - mass_substract_total
    
        this.GetCustomPropertyByPath("Outputs/Mass").Value = Total_Mass_New
        this.GetCustomPropertyByPath("Outputs/Volume").Value = Total_Volume_New
    

    Property Provider part:

    def reload_props():
        this.PropertyProvider = None
    
        provider = Provider()
        group = provider.AddGroup("Outputs")
        double_prop1 = group.AddProperty("Mass", Control.Double)
        double_prop1.CanParameterize = True
        double_prop1.ParameterType = ParameterType.Output    
        double_prop2 = group.AddProperty("Volume", Control.Double)
        double_prop2.CanParameterize = True
        double_prop2.ParameterType = ParameterType.Output    
        this.PropertyProvider = provider
    
    from Ansys.ACT.Mechanical.AdditionalProperties import PropertyProviderAdapter
    from Ansys.ACT.Mechanical.AdditionalProperties import *
    
    from mech_templates import property_templates
    
    property_templates.set_ext_api(ExtAPI)
    
    class Provider(Ansys.ACT.Interfaces.Mechanical.IPropertyProvider):
    
    
        def IsValid(self, prop):
            if(isinstance(prop, DoubleProperty)):
                return prop.ValidRange[0] <= prop.Value and prop.ValidRange[1] >= prop.Value        
            return True
    
        def IsReadOnly(self, prop):
            return False
    
        def IsVisible(self, prop):
            return True
    
        def SetValue(self, prop, val):
            return val
    
        def GetValue(self, prop, val):
            return val
    
        prop_list = []
        prop_map = {}
        prop_groups = set()
    
        class __AnsGroup():
            provider = None
            def __init__(self,name=None, provider=None):
                self.name = name
                self.provider = provider
    
            def __AddScopingProperty(self, name):
                scoping_prop = property_templates.ScopingProperty(name, self.name)
    
                for prop in scoping_prop.GetGroupedProps():
                    self.provider.AddProperty(prop)
                return scoping_prop.GetGroupedProps()
    
            def AddProperty(self, name=None, prop_control=None, module_name=None):
    
                if(prop_control == "Scoping" and module_name == "property_templates"):
                    return self.__AddScopingProperty(name)
    
                prop_mod_globals = None
                if(module_name != None):
                    if(module_name not in globals()):
                        raise Exception("Unknown module : " + module_name)
    
                    prop_mod_globals = globals()[module_name].get_globals()
                else:
                    prop_mod_globals = globals()
    
                prop_class_name = str(prop_control) + "Property"
    
                if(prop_class_name not in prop_mod_globals):
                    raise Exception("Unknown property class : " + prop_class_name)
    
                prop = prop_mod_globals[prop_class_name](self.name + "/" + name, self.name)
    
                if(prop == None):
                    raise Exception("Issue while creating the property instance.")
    
                prop.IsValidCallback = self.provider.IsValid
                prop.IsReadOnlyCallback = self.provider.IsReadOnly
                prop.IsVisibleCallback = self.provider.IsVisible
                prop.GetValueCallback = self.provider.GetValue
                prop.SetValueCallback = self.provider.SetValue
                prop.DisplayName = name           
                self.provider.AddProperty(prop)
    
                return prop
    
        def __init__(self):
            pass
    
        def GetProperties(self):
            return [self.prop_map[propName] for propName in self.prop_list]
    
        def AddGroup(self, name=None):
            if name in self.prop_groups:
                raise Exception("Group with name " + name + " already exists, please use a unique group name.")
    
            self.prop_groups.add(name)
    
            return self.__AnsGroup(name, self)
    
        def AddProperty(self, prop):
            if(prop.Name in self.prop_map):
                raise Exception("Property name must be unique, property with name '" + prop.Name + "' already exisits.")
    
            self.prop_list.append(prop.Name)
            self.prop_map[prop.Name] = prop
    
    reload_props()