外部回路連携用にJCFを出力する。
import designer
def CanUseScript():
app = designer.GetApplication()
studies = CurrentStudies(app)
for study in studies:
isValid = ValidateStudySettings(study)
if isValid:
return True
return False
def CurrentStudies(app):
studies = []
analysisGroup = app.GetCurrentAnalysisGroup()
if analysisGroup.IsValid():
numStudies = analysisGroup.NumStudies()
for i in range(numStudies):
studies.append(analysisGroup.GetStudy(i))
return studies
study = app.GetCurrentStudy()
if study.IsValid():
studies.append(study)
return studies
def ValidateStudySettings(study):
studyType = study.GetScriptTypeName()
if not studyType in [u"Transient2D", u"TransientAX", u"Transient2DResponse", u"Transient", u"TransientResponse"]:
return False
if not study.HasCircuit():
return False
circuit = study.GetCircuit()
isValid = ValidateCircuitComponent(circuit)
if isValid:
return True
numSubCircuit = circuit.NumSubCircuit()
for i in range(numSubCircuit):
subCircuit = circuit.GetSubCircuit(i)
isValid = ValidateCircuitComponent(subCircuit)
if isValid:
return True
return False
def ValidateCircuitComponent(circuit):
for componentTypeName in [u"VoltageSource", u"3PhaseVoltageSource", u"MultiPhaseVoltageSource", u"CurrentSource", u"3PhaseCurrentSource", u"MultiPhaseCurrentSource"]:
numComponents = circuit.NumComponentsByType(componentTypeName)
if numComponents > 0:
return True
return False
class InputDialog:
def __init__(self, app):
self._dialog = app.CreateDialogBox()
titleEN = u"Export Jcf For External Circuit Link"
titleJP = u"外部回路連携用のJCF出力"
self._dialog.SetTranslation(titleEN, titleJP)
self._dialog.SetTitle(titleEN)
labelOutputDirectoryEN = u"Output Directory"
labelOutputDirectoryJP = u"出力ディレクトリ"
self._dialog.SetTranslation(labelOutputDirectoryEN, labelOutputDirectoryJP)
self._varNameOutputDirectory = "outputDirectory"
self._dialog.AddDirectoryPath(self._varNameOutputDirectory, labelOutputDirectoryEN, u"")
self._dialog.AddLine()
labelExternalLinkTypeEN = u"External Circuit Link Type"
labelExternalLinkTypeJP = u"外部回路連携タイプ"
self._dialog.SetTranslation(labelExternalLinkTypeEN, labelExternalLinkTypeJP)
self._dialog.AddLabel(labelExternalLinkTypeEN)
labelExternalLink1EN = u"External Circuit Link 1"
labelExternalLink1JP = u"外部回路連携1"
self._dialog.SetTranslation(labelExternalLink1EN, labelExternalLink1JP)
labelExternalLink2JP = u"外部回路連携2"
labelExternalLink2EN = u"External Circuit Link 2"
self._dialog.SetTranslation(labelExternalLink2EN, labelExternalLink2JP)
self._varNameExternalCircuitLinkType = u"externalCircuitLinkType"
self._dialog.AddRadio(self._varNameExternalCircuitLinkType, labelExternalLink1EN, 1)
self._dialog.AddRadio(self._varNameExternalCircuitLinkType, labelExternalLink2EN, 2)
self._dialog.AddLine()
labelSubcyclingEN = u"Use Subcycling"
labelSubcyclingJP = u"サブサイクリングを使用する"
self._dialog.SetTranslation(labelSubcyclingEN, labelSubcyclingJP)
self._varNameUseSubcycling = u"useSubcycling"
self._dialog.AddCheckBox(self._varNameUseSubcycling, labelSubcyclingEN, 0)
labelMagAnalysisTimingEN = u"TIming for Magnetic Field Analysis"
labelMagAnalysisTimingJP = u"磁界解析のタイミング"
self._dialog.SetTranslation(labelMagAnalysisTimingEN, labelMagAnalysisTimingJP)
labelTimeIntervalEN = u"Time Interval"
labelTimeIntervalJP = u"時間刻み"
self._dialog.SetTranslation(labelTimeIntervalEN, labelTimeIntervalJP)
self._varNameTimeInterval = u"timeInterval"
self._dialog.AddReal(self._varNameTimeInterval, labelTimeIntervalEN, 0.0)
self._dialog.SetMinimum(self._varNameTimeInterval, 0.0)
labelRatedCurrentEN = u"Rated Current"
labelRatedCurrentJP = u"定格電流"
self._varNameRatedCurrent = u"ratedCurrent"
self._dialog.SetTranslation(labelRatedCurrentEN, labelRatedCurrentJP)
self._dialog.AddReal(self._varNameRatedCurrent, labelRatedCurrentEN, 0.0)
self._dialog.SetMinimum(self._varNameRatedCurrent, 0.0)
def Create(self):
self._dialog.Show()
if self._dialog.WasCancelled():
return False
return True
def GetInputParameters(self):
inputParameters = InputParameters()
inputParameters.outputDirectory = self._dialog.GetValue(self._varNameOutputDirectory)
inputParameters.externalCircuitLinkType = self._dialog.GetValue(self._varNameExternalCircuitLinkType)
inputParameters.useSubcycling = self._dialog.GetValue(self._varNameUseSubcycling)
inputParameters.timeInterval = self._dialog.GetValue(self._varNameTimeInterval)
inputParameters.ratedCurrent = self._dialog.GetValue(self._varNameRatedCurrent)
return inputParameters
class InputParameters:
def __init__(self):
self.outputDirectory = u""
self.externalCircuitLinkType = 0
self.useSubcycling = 0
self.timeInterval = 0.0
self.ratedCurrent = 0.0
class ExternalCircuitLinkTarget():
def __init__(self):
self.motion = []
self.motionType = []
self.torque = []
self.force = []
self.currentSource = []
self.voltageSource = []
self.switch = []
self.probe = []
def ConvertForExternalCircuitLink(self, study):
self._ConvertStep(study)
self._ConvertConditions(study)
self._ConvertCircuit(study)
return
def AddExternalCircuitLink(self, study, inputParameters):
useSubcycling = 0
if inputParameters.externalCircuitLinkType == 1:
externalCircuit = study.CreateCondition(u"ExternalCircuitCouple1", u"untitled")
if inputParameters.useSubcycling == 1:
externalCircuit.GetSubCondition(0).SetValue(u"TimeUnit", inputParameters.timeInterval)
externalCircuit.GetSubCondition(0).SetValue(u"RatedCurrent", inputParameters.ratedCurrent)
externalCircuit.GetSubCondition(0).SetValue(u"UseSubcycling", 1)
useSubcycling = 1
else:
externalCircuit = study.CreateCondition(u"ExternalCircuitCouple2", u"untitled")
externalCircuit.GetSubCondition(0).SetValue(u"TimeUnit", inputParameters.timeInterval)
externalCircuit.GetSubCondition(0).SetValue(u"RatedCurrent", inputParameters.ratedCurrent)
useSubcycling = 1
numSubCondition = 0
if inputParameters.externalCircuitLinkType == 1:
for (motion, motionType) in zip(self.motion, self.motionType):
if motionType == u"Rotation":
numSubCondition += 1
else:
numSubCondition += 3
numSubCondition += len(self.torque)
numSubCondition += len(self.force)*3
numSubCondition += len(self.currentSource)
numSubCondition += len(self.voltageSource)
numSubCondition += len(self.probe)
numSubCondition += len(self.switch)
else:
numSubCondition += len(self.currentSource)
numSubCondition += len(self.voltageSource)
subConditionName = u"ExternalCircuitComponent"
for i in range(numSubCondition):
externalCircuit.CreateSubCondition(subConditionName, u"untitled")
subConditionCounter = 1
for curSource in self.currentSource:
subCondition = externalCircuit.GetSubCondition(subConditionCounter)
subCondition.SetLink(curSource)
subCondition.SetValue(u"inout", 1)
subConditionCounter += 1
for volSource in self.voltageSource:
subCondition = externalCircuit.GetSubCondition(subConditionCounter)
subCondition.SetLink(volSource)
subCondition.SetValue(u"inout", 1)
subConditionCounter += 1
if inputParameters.externalCircuitLinkType == 1:
for (motion, motionType) in zip(self.motion, self.motionType):
if motionType == u"Rotation":
subCondition = externalCircuit.GetSubCondition(subConditionCounter)
subCondition.SetLink(motion.GetName())
subCondition.SetValue(u"type", 4)
subCondition.SetValue(u"inout", 1)
subConditionCounter += 1
else:
subCondition = externalCircuit.GetSubCondition(subConditionCounter)
subCondition.SetLink(motion.GetName())
subCondition.SetValue(u"type", 1)
subCondition.SetValue(u"inout", 1)
subConditionCounter += 1
subCondition = externalCircuit.GetSubCondition(subConditionCounter)
subCondition.SetLink(motion.GetName())
subCondition.SetValue(u"type", 2)
subCondition.SetValue(u"inout", 1)
subConditionCounter += 1
subCondition = externalCircuit.GetSubCondition(subConditionCounter)
subCondition.SetLink(motion.GetName())
subCondition.SetValue(u"type", 3)
subCondition.SetValue(u"inout", 1)
subConditionCounter += 1
for torque in self.torque:
subCondition = externalCircuit.GetSubCondition(subConditionCounter)
subCondition.SetLink(torque.GetName())
subCondition.SetValue(u"type", 5)
subConditionCounter += 1
for force in self.force:
subConditionX = externalCircuit.GetSubCondition(subConditionCounter)
subConditionX.SetLink(force.GetName())
subConditionX.SetValue(u"type", 1)
subConditionCounter += 1
subConditionY = externalCircuit.GetSubCondition(subConditionCounter)
subConditionY.SetLink(force.GetName())
subConditionY.SetValue(u"type", 2)
subConditionCounter += 1
subConditionZ = externalCircuit.GetSubCondition(subConditionCounter)
subConditionZ.SetLink(force.GetName())
subConditionZ.SetValue(u"type", 3)
subConditionCounter += 1
for probe in self.probe:
subCondition = externalCircuit.GetSubCondition(subConditionCounter)
subCondition.SetLink(probe)
subConditionCounter += 1
for switch in self.switch:
subCondition = externalCircuit.GetSubCondition(subConditionCounter)
subCondition.SetLink(switch)
subCondition.SetValue(u"inout", 1)
subConditionCounter += 1
if useSubcycling == 1:
numMoverComponent = 2*len(self.motion)
moverComponentName = u"MoverComponent"
for i in range(numMoverComponent):
externalCircuit.CreateSubCondition(moverComponentName, u"untitled")
moverCounter = 1
for (motion, motionType) in zip(self.motion, self.motionType):
moverName = u"Undefined " + str(moverCounter)
subCondition = externalCircuit.GetSubCondition(subConditionCounter)
subCondition.SetValue(u"name", moverName)
subCondition.SetLinkWithType(u"designer_motion_id", motion.GetName())
motionRegion = motion.GetSelection()
numMotionRegionPart = motionRegion.NumParts()
motionPartIds = []
for imotionPart in range(numMotionRegionPart):
motionPartIds.append(motionRegion.PartID(imotionPart))
motionPartIds.sort()
subConditionCounter += 1
if motionType == u"Rotation":
candidates = self.torque
else:
candidates = self.force
for force in candidates:
forceRegion = force.GetSelection()
numForceRegionPart = forceRegion.NumParts()
targetType = 0
forceConditionType = force.GetScriptTypeName()
if (forceConditionType not in [u"LorentzTorque", u"LorentzForce"]):
targetType = force.GetValue(u"TargetType")
if targetType == 1:
linkedMotion = force.GetLink()
if motion.GetName() != linkedMotion:
continue
subConditionForce = externalCircuit.GetSubCondition(subConditionCounter)
subConditionForce.SetValue(u"name", moverName)
subConditionForce.SetLinkWithType(u"deisnger_force_id", force.GetName())
subConditionCounter += 1
break
else:
forcePartIds = []
for iforcePart in range(numForceRegionPart):
forcePartIds.append(forceRegion.PartID(iforcePart))
forcePartIds.sort()
if motionPartIds != forcePartIds:
continue
subConditionForce = externalCircuit.GetSubCondition(subConditionCounter)
subConditionForce.SetValue(u"name", moverName)
subConditionForce.SetLinkWithType(u"deisnger_force_id", force.GetName())
subConditionCounter += 1
break
moverCounter += 1
return True
def _ConvertStep(self, study):
step = study.GetStep()
step.SetValue(u"Step", 1)
step.SetValue(u"StepType", 5)
return
def _ConvertConditions(self, study):
numConditions = study.NumConditions()
for i in range(numConditions):
condition = study.GetCondition(i)
conditionType = condition.GetScriptTypeName()
if conditionType in [u"RotationMotion", u"TranslationMotion"]:
condition.SetValue(u"MotionGroupType", 6)
self.motion.append(condition)
if conditionType == u"RotationMotion":
self.motionType.append(u"Rotation")
else:
self.motionType.append(u"Translation")
if conditionType in [u"Torque", u"Torque_Surface", u"LorentzTorque"]:
self.torque.append(condition)
if conditionType in [u"Force", u"Force_Surface", u"LorentzForce"]:
self.force.append(condition)
return
def _ConvertCircuit(self, study):
circuit = study.GetCircuit()
self._ConvertCircuitComponent(circuit)
return
def _ConvertCircuitComponent(self, circuit):
self.currentSource.extend(self._ConvertSource(circuit, u"Current"))
self.voltageSource.extend(self._ConvertSource(circuit, u"Voltage"))
self.probe.extend(self._CollectProbe(circuit, u"CurrentProbe"))
self.probe.extend(self._CollectProbe(circuit, u"VoltageProbe"))
numSwitches = circuit.NumComponentsByType(u"Switch")
for i in range(numSwitches):
component = circuit.GetComponentByType(u"Switch", i)
component.SetValue(u"TimingType", 3)
self.switch.append(component.GetName())
return
def _ConvertSource(self, circuit, typeName):
srcNames = []
if typeName == u"Current":
oneTerminal = u"CurrentSource"
threePhase = u"3PhaseCurrentSource"
multiPhase = u"MultiPhaseCurrentSource"
convertedTypeName = u"CurrentSource"
elif typeName == u"Voltage":
oneTerminal = u"VoltageSource"
threePhase = u"3PhaseVoltageSource"
multiPhase = u"MultiPhaseVoltageSource"
convertedTypeName = u"VoltageSource"
else:
return srcNames
numConvert = 1
numSrcs = circuit.NumComponentsByType(oneTerminal)
for i in range(numSrcs):
component = circuit.GetComponentByType(oneTerminal, i)
component.SetValue(u"FunctionType", 7)
srcNames.append(component.GetName())
numConvert += 1
num3PhaseSrcs = circuit.NumComponentsByType(threePhase)
for i in range(num3PhaseSrcs):
componentInstance = circuit.GetComponentInstanceByType(threePhase, i)
position = componentInstance.GetPosition()
posX = position.GetX()
posY = position.GetY()
for j in range(3):
newComponentName = u"convertedFrom" + threePhase + str(numConvert)
newComponent = circuit.CreateComponent(convertedTypeName, newComponentName)
newComponent.SetValue(u"FunctionType", 7)
circuit.CreateInstance(newComponentName, posX, posY + 2*(1-j))
srcNames.append(newComponentName)
numConvert += 1
circuit.DeleteComponentInstanceByType(threePhase, i)
numMultiPhaseSrc = circuit.NumComponentsByType(multiPhase)
for i in range(numMultiPhaseSrc):
component = circuit.GetComponentByType(multiPhase, i)
numPhase = int(component.GetValue(u"NumPhases"))
componentInstance = circuit.GetComponentInstanceByType(multiPhase, i)
position = componentInstance.GetPosition()
posX = position.GetX()
posY = position.GetY()
for j in range(numPhase):
newComponentName = u"convertedFrom" + multiPhase + str(numConvert)
newComponent = circuit.CreateComponent(convertedTypeName, newComponentName)
newComponent.SetValue(u"FunctionType", 7)
circuit.CreateInstance(newComponentName, posX, posY + 2*(1-j) + 1)
srcNames.append(newComponentName)
numConvert += 1
circuit.DeleteComponentInstanceByType(multiPhase, i)
numMacroComponent = circuit.NumComponentsByType(u"MacroComponent")
for i in range(numMacroComponent):
subCircuit = circuit.GetSubCircuit(i)
numComponents = subCircuit.NumComponentsByType(typeName + u"Source")
if (numComponents == 3):
subCircuitInstance = circuit.GetComponentInstanceByType(u"MacroComponent", 0)
position = subCircuitInstance.GetPosition()
posX = position.GetX()
posY = position.GetY()
for j in range(3):
newComponentName = u"convertedFrom" + threePhase + str(numConvert)
newComponent = circuit.CreateComponent(convertedTypeName, newComponentName)
newComponent.SetValue(u"FunctionType", 7)
circuit.CreateInstance(newComponentName, posX, posY + 2*(1-j) + 1)
srcNames.append(newComponentName)
numConvert += 1
circuit.DeleteComponentInstanceByType(u"MacroComponent", i)
return srcNames
def _CollectProbe(self, circuit, typeName):
srcNames = []
numProbes = circuit.NumComponentsByType(typeName)
for i in range(numProbes):
component = circuit.GetComponentByType(typeName, i)
srcNames.append(component.GetName())
return srcNames
def AddExternalCircuitLink(study, inputParameters):
externalCircuitLinkTarget = ExternalCircuitLinkTarget()
externalCircuitLinkTarget.ConvertForExternalCircuitLink(study)
return externalCircuitLinkTarget.AddExternalCircuitLink(study, inputParameters)
def ShowErrorMessage(app, messageEN, messageJP):
msgdlg = app.CreateDialogBox()
msgdlg.SetTranslation(messageEN, messageJP)
msgdlg.SetCancelButtonVisible(False)
msgdlg.AddLabel(messageEN)
msgdlg.Show()
def Main():
app = designer.GetApplication()
studies = CurrentStudies(app)
validStudies = []
for study in studies:
isValid = ValidateStudySettings(study)
if isValid:
validStudies.append(study)
if len(validStudies) == 0:
msgdlg = app.CreateDialogBox()
messageEN = u"""There is no supported study.To use this function, following settings are required.
1. Study type is magnetic transient study (except for section study) or efficiency map study.
2. Study has circuit.
3. Current/Voltage source is included in circuit."""
messageJP = u"""サポートしているスタディがありません。本機能を実行するためには,以下の条件を満たす必要があります。
1. スタディタイプが磁界過渡応答(断面過渡応答を除く)または効率マップスタディである。
2. スタディに回路が存在する。
3. 電流源または電圧源が回路に存在する。"""
msgdlg.SetTranslation(messageEN, messageJP)
msgdlg.SetCancelButtonVisible(False)
msgdlg.AddLabel(messageEN)
msgdlg.Show()
return
dialog = InputDialog(app)
if not dialog.Create():
return
inputParameters = dialog.GetInputParameters()
if len(inputParameters.outputDirectory) == 0:
messageEN = u"Please specify the output directory."
messageJP = u"出力ディレクトリを設定してください。"
ShowErrorMessage(app, messageEN, messageJP)
return
if (inputParameters.useSubcycling == 1) or (inputParameters.externalCircuitLinkType == 2):
if inputParameters.timeInterval <= 0.0:
messageEN = u"Please specify the value greater than zero to time interval."
messageJP = u"時間刻みにはゼロより大きな値を設定してください。"
ShowErrorMessage(app, messageEN, messageJP)
return
currentModel = app.GetCurrentModel()
for validStudy in validStudies:
studyName = validStudy.GetName()
studyWithExternalCircuitLink = currentModel.DuplicateStudy(studyName)
if not studyWithExternalCircuitLink.IsValid():
messageEN = u"Temporary study cannot be created."
messageJP = u"一時スタディの生成に失敗しました。"
ShowErrorMessage(app, messageEN, messageJP)
return
if not AddExternalCircuitLink(studyWithExternalCircuitLink, inputParameters):
messageEN = u"Error occurred."
messageJP = u"エラーが発生しました。"
ShowErrorMessage(app, messageEN, messageJP)
return
designTable = studyWithExternalCircuitLink.GetDesignTable()
numCases = designTable.NumCases()
for icase in range(numCases):
if not designTable.IsActive(icase):
continue
studyWithExternalCircuitLink.SetCurrentCase(icase)
if not studyWithExternalCircuitLink.HasMesh():
studyWithExternalCircuitLink.CreateMesh()
try:
jcfFileName = inputParameters.outputDirectory.decode() + "/" + studyName + "_case" + str(icase+1) + ".jcf"
except:
jcfFileName = inputParameters.outputDirectory + "/" + studyName + "_case" + str(icase+1) + ".jcf"
studyWithExternalCircuitLink.WriteMeshJcf(jcfFileName)
numStudies = currentModel.NumStudies()
currentModel.DeleteStudy(numStudies-1)
return
if __name__ == '__main__':
Main()


