Let's imagine that we want to create 4 sine dwells at 28, 35, 52, 236 Hz of different amplitudes and 1 sine sweep between 10 and 500Hz. Would it be possible to do it with Mechanical scripting?
Important Note: Embedded DesignLife APIs are not officially supported, so check that the script behaves as expected. If a property change doesn't update the object, call object.NotifyChange().
Keeping that in mind, the code below shows how to set up the described loading using the modal FRF approach.
# ---------------------------------------------------------------- user inputs MODAL_ANALYSIS_INDEX = 0 HARMONIC_ANALYSIS_INDEX = 1 SINGLE_FREQ_FREQUENCIES= [28, 35, 52, 236] # [Hz] SINGLE_FREQ_AMPLITUDES = [0.5, 1.0, 1.5, 2.0] SINGLE_FREQ_EXPOSURE = 300.0 # [s] SWEEP_START_FREQ = 10.0 # [Hz] SWEEP_END_FREQ = 500.0 # [Hz] SWEEP_AMPLITUDE = 1.0 SWEEP_TYPE = 'Log (octaves/minute)' SWEEP_RATE = 1.0 SWEEP_COUNT = 1 EXTENSION_NAME = 'MechanicalEmbeddedDesignLife' # ------------------------------------------------------------------- helpers def find_load_mapper(): """Return the Load Mapper (DutyCycle) of the last DesignLife analysis in the tree.""" load_mapper = None for analysis in Model.Analyses: try: user_objects = analysis.GetUserObjects(EXTENSION_NAME) except Exception: continue for user_obj in user_objects: if user_obj.Name == "DutyCycle": load_mapper = user_obj return load_mapper def get_analysis(index, expected_type, label): analyses = Model.Analyses if index < 0 or index >= analyses.Count: raise Exception("%s analysis index %d out of range (found %d analyses)." % (label, index, analyses.Count)) analysis = analyses[index] if analysis.AnalysisType != expected_type: raise Exception("Analysis at index %d ('%s') is %s, expected %s." % (index, analysis.Name, analysis.AnalysisType, expected_type)) return analysis def set_selection(prop, analysis): """Set an environment selection property, trying the identifiers DesignLife may expect.""" for candidate in (str(analysis.ObjectId), analysis.Name): try: prop.Value = candidate except Exception: continue if str(prop.Value) == candidate: return raise Exception("Could not set '%s' to analysis '%s'." % (prop.Name, analysis.Name)) def get_prop(obj, name): prop = obj.Properties[name] if prop is None: raise Exception("Property '%s' not found on '%s'." % (name, obj.Name)) return prop def set_environments(load, modal_analysis, harmonic_analysis): get_prop(load, 'RSTEnvironmentDefinition').Value = 'Environment' set_selection(get_prop(load, 'RSTEnvironmentDefinition/Environment'), modal_analysis) get_prop(load, 'MCFEnvironmentDefinition').Value = 'Environment' set_selection(get_prop(load, 'MCFEnvironmentDefinition/MCFEnvironment'), harmonic_analysis) def create_loading_events(load_mapper, count): """Return `count` loading events: reuse empty ones (e.g. the default event), create the rest.""" events = [e for e in load_mapper.Children if len(list(e.Children)) == 0][:count] while len(events) < count: try: events.append(load_mapper.CreateChild('nCodeObj')) except Exception as ex: raise Exception("Could not create loading event %d of %d (%s). Delete the loading events " "left over from previous runs in the Load Mapper and run the script again." % (len(events) + 1, count, ex)) return events def configure_loading_event(loading_event, freq_type): get_prop(loading_event, 'LoadDomain').Value = 'Frequency' get_prop(loading_event, 'LoadDomain/FreqType').Value = freq_type loading_event.NotifyChange() def create_frequency_load(loading_event): """createChildFreq does not return the new load, so fetch it from the event's children.""" existing_ids = set(child.Id for child in loading_event.Children) designlife_script_scope.createChildFreq(loading_event) new_children = [c for c in loading_event.Children if c.Id not in existing_ids] if new_children: return new_children[-1] return loading_event.CreateChild('FatigueLoadFreq') def fill_table(table_prop, rows): """Fill an ACT worksheet (table) property with (frequency, value) rows.""" while table_prop.RowCount > 0: table_prop.DeleteRow(0) for freq, value in rows: table_prop.AddRow() table_prop.Properties['TableFreq'].Value = freq table_prop.Properties['TableValue'].Value = value table_prop.SaveActiveRow() # ----------------------------------------------------------------- Load Mapper load_mapper = find_load_mapper() if load_mapper is None: raise Exception("No Embedded DesignLife Load Mapper found. Insert a DesignLife analysis first.") designlife_script_scope = ExtAPI.ExtensionManager.GetExtensionByName(EXTENSION_NAME).ScriptScope modal_analysis = get_analysis(MODAL_ANALYSIS_INDEX, AnalysisType.Modal, "Modal") harmonic_analysis = get_analysis(HARMONIC_ANALYSIS_INDEX, AnalysisType.Harmonic, "Harmonic") # One loading event per load, all created before any load is added loading_events = create_loading_events(load_mapper, len(SINGLE_FREQ_FREQUENCIES) + 1) # --------------------------------------------------------- Single frequency loads single_freq_loads = [] for loading_event, frequency, amplitude in zip(loading_events, SINGLE_FREQ_FREQUENCIES, SINGLE_FREQ_AMPLITUDES): configure_loading_event(loading_event, 'Single Frequency') sf_load = create_frequency_load(loading_event) set_environments(sf_load, modal_analysis, harmonic_analysis) sf_load.Properties['Freq'].Value = frequency sf_load.Properties['AmplutideFreq'].Value = amplitude # (sic) property name in the API sf_load.Properties['ExposureFreq'].Value = SINGLE_FREQ_EXPOSURE sf_load.NotifyChange() single_freq_loads.append(sf_load) # ------------------------------------------------------------- Sine sweep load loading_event = loading_events[-1] configure_loading_event(loading_event, 'Frequency Range') sweep_load = create_frequency_load(loading_event) set_environments(sweep_load, modal_analysis, harmonic_analysis) fill_table(sweep_load.Properties['TableDefinitionRange'], [(SWEEP_START_FREQ, SWEEP_AMPLITUDE), (SWEEP_END_FREQ, SWEEP_AMPLITUDE)]) sweep_load.Properties['SweepType'].Value = SWEEP_TYPE sweep_load.Properties['SweepRate'].Value = SWEEP_RATE sweep_load.Properties['NumberOfSweeps'].Value = SWEEP_COUNT sweep_load.NotifyChange() print("Created %d single frequency loads (frequencies %s, amplitudes %s) and a %g-%g Hz sine sweep (amplitude %g), each in its own loading event." % (len(single_freq_loads), SINGLE_FREQ_FREQUENCIES, SINGLE_FREQ_AMPLITUDES, SWEEP_START_FREQ, SWEEP_END_FREQ, SWEEP_AMPLITUDE)) print("Environment (RST): '%s' | MCF Environment: '%s'" % (modal_analysis.Name, harmonic_analysis.Name))