Skip to content

Commit

Permalink
Added support for both slx and mdl Simulink model export
Browse files Browse the repository at this point in the history
  • Loading branch information
imbur committed Feb 9, 2015
1 parent dcf5a4b commit ee386e3
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public void exampleExport() throws SimulinkApiException, IOException {
SimulinkModel loadedModel = exporter.loadSimulinkModel(modelPath + modelName);

exporter.export(loadedModel, commandFactory);
exporter.saveSimulinkModel(loadedModel.getSimulinkRef().getFQN());
String fqn = loadedModel.getSimulinkRef().getFQN();
exporter.saveSimulinkModel(fqn.substring(0,fqn.lastIndexOf('.')),"slx");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public SimulinkModel loadSimulinkModel(String fileNameWithoutExtension) throws S
* the exported model's save path
* @throws SimulinkApiException
*/
public void saveSimulinkModel(String modelNameWithPath) throws SimulinkApiException {
public void saveSimulinkModel(String modelNameWithPath, String fileExtension) throws SimulinkApiException {

// If no command evaluator object is assigned yet, indicate it
if (commandFactory == null) {
Expand All @@ -193,7 +193,7 @@ public void saveSimulinkModel(String modelNameWithPath) throws SimulinkApiExcept
}

// Save the model
MatlabCommand saveSystem = commandFactory.saveSystem().addParam(modelName).addParam(modelName + ".mdl");
MatlabCommand saveSystem = commandFactory.saveSystem().addParam(modelName).addParam(modelName + "." + fileExtension);
saveSystem.execute();

// Navigate back to the original working directory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
Expand All @@ -31,6 +32,8 @@ public abstract class AbstractSimulinkSettingsDialog extends Dialog {
private ContainerFieldEditor targetDirectoryEditor;
private String dialogTitle;

protected IPreferenceStore store;

public AbstractSimulinkSettingsDialog(Shell parentShell, String dialogTitle, File targetDirectory) {
super(parentShell);
this.dialogTitle = dialogTitle;
Expand Down Expand Up @@ -64,7 +67,7 @@ protected void createFieldEditors() {
targetDirectoryEditor.setStringValue(targetDirectory.getPath());
addField(targetDirectoryEditor);

List<FieldEditor> additionalFields = additionalFields(fieldEditorParent);
List<? extends FieldEditor> additionalFields = additionalFields(fieldEditorParent);
for (FieldEditor fieldEditor : additionalFields) {
addField(fieldEditor);
}
Expand All @@ -83,7 +86,7 @@ protected void updateApplyButton() {
return pageControl;
}

protected abstract List<FieldEditor> additionalFields(Composite fieldEditorParent);
protected abstract List<? extends FieldEditor> additionalFields(Composite fieldEditorParent);

@Override
protected void createButtonsForButtonBar(Composite parent) {
Expand All @@ -110,4 +113,8 @@ public File getTargetDirectory() {
return targetDirectory;
}

public void setPreferenceStore(IPreferenceStore store) {
this.store = store;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,53 @@
*******************************************************************************/
package hu.bme.mit.massif.simulink.importer.ui.dialogs;

import hu.bme.mit.massif.simulink.importer.ui.preferences.PreferenceConstants;

import java.io.File;
import java.util.Collections;
import java.util.List;

import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;

import com.google.common.collect.Lists;

public class ExportSettingsDialog extends AbstractSimulinkSettingsDialog {
private RadioGroupFieldEditor modelExtensionSelector;


public ExportSettingsDialog(Shell parentShell, File targetDirectory) {
super(parentShell, "Export Parameters", targetDirectory);
}

@Override
protected List<FieldEditor> additionalFields(Composite fieldEditorParent) {
return Collections.emptyList();
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setSize(500, 192);
}


@Override
protected List<? extends FieldEditor> additionalFields(Composite fieldEditorParent) {
modelExtensionSelector = new RadioGroupFieldEditor(
PreferenceConstants.EXPORT_RESULT_MODEL_EXTENSION,
"Simulink model file extension: ",
1,
new String[][] {
{"MDL", "mdl"},
{"SLX", "slx"}
},
fieldEditorParent);
//@formatter:on
modelExtensionSelector.setPreferenceStore(store);
modelExtensionSelector.load();

return Lists.newArrayList(modelExtensionSelector);
}

public void storeSelectedFileExtension() {
modelExtensionSelector.setPreferenceStore(store);
modelExtensionSelector.store();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
Expand All @@ -42,7 +41,7 @@ public Map<String, Boolean> getSelectedFiltersById() {
}

private StringFieldEditor importedModelNameEditor;
private IPreferenceStore store;


public ImportSettingsDialog(Shell parentShell, String exporterArgs, File targetDirectory) {
super(parentShell, " Import Parameters", targetDirectory);
Expand Down Expand Up @@ -100,7 +99,4 @@ protected List<FieldEditor> additionalFields(Composite fieldEditorParent) {
return fes;
}

public void setPreferenceStore(IPreferenceStore store) {
this.store = store;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;
Expand Down Expand Up @@ -83,13 +85,17 @@ private void handleFile(final ICommandEvaluator evaluator, IFile file) {

ExportSettingsDialog dialog = new ExportSettingsDialog(Display.getCurrent().getActiveShell(),
new File(modelPath));
IPreferenceStore store = MassifSimulinkUIPlugin.getDefault().getPreferenceStore();
dialog.setPreferenceStore(store);
dialog.open();

// If the operation was cancelled, then return
if (dialog.getReturnCode() == Dialog.CANCEL) {
return;
}

dialog.storeSelectedFileExtension();

resultPath = dialog.getTargetDirectory().toString();
}
// TODO is final needed? If the model is moved, finalResultPath stays unchanged
Expand Down Expand Up @@ -144,7 +150,9 @@ protected IStatus run(IProgressMonitor monitor) {
String exportedModelName = finalResultPath + File.separator + newModelName;

try {
exporter.saveSimulinkModel(exportedModelName);
IPreferenceStore store = MassifSimulinkUIPlugin.getDefault().getPreferenceStore();
String extension = store.getString(PreferenceConstants.EXPORT_RESULT_MODEL_EXTENSION);
exporter.saveSimulinkModel(exportedModelName, extension);
} catch (SimulinkApiException e) {
Status status = new Status(Status.ERROR, MassifSimulinkUIPlugin.PLUGIN_ID, EXCEPTION_WHILE_SAVING, e);
MassifSimulinkUIPlugin.getDefault().getLog().log(status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
Expand Down Expand Up @@ -53,6 +54,20 @@ public void createFieldEditors() {
PreferenceConstants.EXPORT_RESULT_MODEL_PATH, "Default result model location:", getFieldEditorParent());
addField(resultModelPathEditor);

// Result model extension
//@formatter:off
RadioGroupFieldEditor modelExtensionSelector= new RadioGroupFieldEditor(
PreferenceConstants.EXPORT_RESULT_MODEL_EXTENSION,
"Simulink model file extension: ",
1,
new String[][] {
{"MDL", "mdl"},
{"SLX", "slx"}
},
getFieldEditorParent());
//@formatter:on
addField(modelExtensionSelector);

// A horizontal separator on the page
SeparatorFieldEditor separator = new SeparatorFieldEditor(getFieldEditorParent());
addField(separator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class PreferenceConstants {

// Export
public static final String EXPORT_RESULT_MODEL_PATH = "exportResultModelPath";
public static final String EXPORT_RESULT_MODEL_EXTENSION = "exportResultModelExtension";
public static final String EXPORT_SCRIPT_NAMES = "exportScriptNames";
public static final String EXPORT_ADDITIONAL_MATLAB_PATH = "exportAdditionalMatlabPath";
public static final String MATLAB_CONTROL_ID = "massif.connectors.matlab.control";
Expand Down

0 comments on commit ee386e3

Please sign in to comment.