/* * Copyright (c) 2014 NoMagic, Inc. All Rights Reserved. */ package com.nomagic.magicdraw.examples.imagegenerator; import com.nomagic.magicdraw.commandline.CommandLine; import com.nomagic.magicdraw.core.Application; import com.nomagic.magicdraw.core.Project; import com.nomagic.magicdraw.core.project.ProjectDescriptor; import com.nomagic.magicdraw.core.project.ProjectDescriptorsFactory; import com.nomagic.magicdraw.core.project.ProjectsManager; import com.nomagic.magicdraw.export.image.ImageExporter; import com.nomagic.magicdraw.uml.symbols.DiagramPresentationElement; import java.io.File; import java.io.IOException; /** * Command line utility for diagram images exporting. * See the Developer guide for startup command sample. */ public class ExportDiagramImages extends CommandLine { private static final String PROJECT = "project_file"; private static final String DESTINATION = "destination_dir"; private static String mProjectFile; private static File mDestinationDir; @Override protected byte execute() { final File file = new File(mProjectFile); final ProjectDescriptor projectDescriptor = ProjectDescriptorsFactory.createProjectDescriptor(file.toURI()); if (projectDescriptor == null) { System.out.println("Project descriptor was not created for " + file.getAbsolutePath()); return -1; } final ProjectsManager projectsManager = Application.getInstance().getProjectsManager(); projectsManager.loadProject(projectDescriptor, true); final Project project = projectsManager.getActiveProject(); if (project == null) { System.out.println("Project " + file.getAbsolutePath() + " was not loaded."); return -1; } for (DiagramPresentationElement diagram : project.getDiagrams()) { final File diagramFile = new File(mDestinationDir, diagram.getHumanName() + diagram.getID() + ".svg"); try { ImageExporter.export(diagram, ImageExporter.SVG, diagramFile); } catch (IOException e) { e.printStackTrace(); } } return 0; } @Override protected void parseArgs(String[] args) throws Exception { for (final String argument : args) { if (!checkArgument(argument, PROJECT)) { checkArgument(argument, DESTINATION); } } /* * Check project file. */ mProjectFile = System.getProperty(PROJECT, ""); if (mProjectFile.length() == 0) { System.out.println("Project file not defined!"); throw new IllegalArgumentException(); } /* * Check destination dir name. */ final String destinationDirName = System.getProperty(DESTINATION, ""); if (destinationDirName.length() == 0) { System.out.println("Destination directory not defined!"); throw new IllegalArgumentException(); } /* * Check destination dir. */ mDestinationDir = new File(destinationDirName); if (!mDestinationDir.exists() && !mDestinationDir.mkdirs()) { System.out.println("Cannot create destination directory!"); throw new IllegalArgumentException(); } System.out.println("Exporting images..."); } /** * Check the given argument and set the system property. * * @param arg argument. * @param propertyName property name. * @return true if given argument is describes given property, otherwise - false. */ private static boolean checkArgument(String arg, String propertyName) { final String start = propertyName + "="; if (arg.startsWith(start)) { System.setProperty(propertyName, arg.substring(start.length())); return true; } return false; } }