Submit a problem

In order to provide you the best help possible to analyze and debug your problems please provide us some information about your environment.
When you submit a problem, please indicate:
- The Version, Build Id, Core Id, System and Architecture (You can get these information in the Help/About menu entry)
- The Modelio Edition (Modelio Open Source or Modelio by Modeliosoft)
- Your Operating System

file Script for export diagram to other project

11 months 2 weeks ago #4066 by Keitarosama
Hi,

I'm relatively new to Modelio and right now I'm trying to copy a diagram to other project, but I didn't know how to do that.

I'm checking that I can export it to an image, but I can't find where I could export it to another project. I know too, that an export in XMI can pass the elements but not the diagrams.

So, can you explain me a way to do that?, if I make a script, could I make the exportation? Do you have an example to do that?

Please help!

Please Log in or Create an account to join the conversation.

11 months 2 days ago #4083 by pan
Hi,

There is no standard way to copy specifically a diagram to another project. Eventually, you can import a project content using the Modelio import feature .

You can develop a module or a Jython script in order to export diagram data. This is an example how you can do it with Jython :
from java.io import *
from javax.xml.parsers import *
from javax.xml.transform import *
from javax.xml.transform.dom import *
from javax.xml.transform.stream import *
from org.xml.sax import *
from org.w3c.dom import *

session = Modelio.getInstance().getModelingSession()
diagramservice = Modelio.getInstance().getDiagramService()

outputDirectory = "C:\\tmp"

element = selectedElements.get(0)

if (isinstance(element,StaticDiagram)):
	dh = diagramservice.getDiagramHandle(element)
	diagramnode = dh.getDiagramNode()
	nodes = diagramnode.getNodes()
	
	try:
		dbFactory = DocumentBuilderFactory.newInstance()
		dBuilder = dbFactory.newDocumentBuilder()
		doc = dBuilder.newDocument()
		
		# root element
		rootElement = doc.createElement("diagrams")
		doc.appendChild(rootElement)
		
		# insert a diagram element
		diagram = doc.createElement("diagram")
		attrName = doc.createAttribute("name")
		attrName.setValue(element.getName())
		diagram.setAttributeNode(attrName)
		rootElement.appendChild(diagram)

		# insert nodes
		for node in nodes:
			nodeElt = doc.createElement("node")
			attrUuid = doc.createAttribute("uuid")
			attrUuid.setValue(node.getElement().getUuid())
			attrName = doc.createAttribute("name")
			attrName.setValue(node.getElement().getName())
			attrEltMClass = doc.createAttribute("eltMClass")
			attrEltMClass.setValue(node.getElement().getMClass().getName())
			nodeBounds = node.getBounds()
			attrX = doc.createAttribute("x")
			attrX.setValue(str(nodeBounds.x()))
			attrY = doc.createAttribute("y")
			attrY.setValue(str(nodeBounds.y()))
			attrW = doc.createAttribute("width")
			attrW.setValue(str(nodeBounds.width()))
			
			nodeElt.setAttributeNode(attrUuid)
			nodeElt.setAttributeNode(attrName)
			nodeElt.setAttributeNode(attrEltMClass)
			nodeElt.setAttributeNode(attrX)
			nodeElt.setAttributeNode(attrY)
			nodeElt.setAttributeNode(attrW)
			diagram.appendChild(nodeElt)
		
		transformerFactory = TransformerFactory.newInstance()
		transformer = transformerFactory.newTransformer()
		transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8")
		transformer.setOutputProperty(OutputKeys.INDENT, "yes")
		source = DOMSource(doc)
		result = StreamResult(File(outputDirectory+"\\outputdiagram.xml"))
		transformer.transform(source, result)
		
	except Exception, exc:
		print "Exception: ", str(exc)

You can export diagram(s) data from your source project as an xml file then build a diagram on your destination project by importing the xml data.
In this example, I only handled the export process. To work, you have to select a diagram then run this script. Of course, it can be modified so you can manage the selection of several diagrams.

You can consult the Javadoc API to obtain more information about the needed methods and classes.
More specifically you can consult the API of IDiagramNode, IDiagramDG, IDiagramHandle, IDiagramService for more information about diagrams' methods.
This is the wiki about Diagrams services .

Let us know if it helps.

BR

Please Log in or Create an account to join the conversation.

Moderators: chmcma
Time to create page: 0.056 seconds
^ Back to Top