/// <summary> /// Creates and initializes a ConnectorModel from its Xml representation. /// </summary> /// <param name="connEl">XmlElement for a ConnectorModel.</param> /// <param name="nodes">Dictionary to be used for looking up a NodeModel by it's Guid.</param> /// <returns>Returns the new instance of ConnectorModel loaded from XmlElement.</returns> public static CSharpCommonGraph.Edge LoadConnectorsAndAddPortsFromXml(XmlElement connEl, IDictionary <string, CSharpCommonGraph.Node> nodes) { var helper = new Dynamo.Utilities.XmlElementHelper(connEl); var guid = helper.ReadGuid("guid", Guid.NewGuid()); var guidStart = helper.ReadGuid("start"); var guidEnd = helper.ReadGuid("end"); int startIndex = helper.ReadInteger("start_index"); int endIndex = helper.ReadInteger("end_index"); //find the elements to connect CSharpCommonGraph.Node start; if (nodes.TryGetValue(guidStart.ToString(), out start)) { var startport = new CSharpCommonGraph.Port(); startport.InstanceGuid = start.InstanceGuid.ToString() + "_OUT" + startIndex.ToString(); startport.MetaData.Inspect = "start"; startport.Name = "a cool port"; //if we have not seen this port id before add the port, we dont want to duplicate ports if (start.Ports.All(x => x.InstanceGuid != startport.InstanceGuid)) { start.Ports.Add(startport); } CSharpCommonGraph.Node end; if (nodes.TryGetValue(guidEnd.ToString(), out end)) { var endport = new CSharpCommonGraph.Port(); endport.InstanceGuid = end.InstanceGuid.ToString() + "_IN" + endIndex.ToString(); endport.MetaData.Inspect = "end"; endport.Name = "a cool port"; if (end.Ports.All(x => x.InstanceGuid != endport.InstanceGuid)) { end.Ports.Add(endport); } var edge = new CSharpCommonGraph.Edge(); edge.SrcGuid = startport.InstanceGuid; edge.DestGuid = endport.InstanceGuid; edge.InstanceGuid = guid.ToString(); return(edge); } } return(null); }
/// <summary> /// Creates and initializes a ConnectorModel from its Xml representation. /// </summary> /// <param name="connEl">XmlElement for a ConnectorModel.</param> /// <param name="nodes">Dictionary to be used for looking up a NodeModel by it's Guid.</param> /// <returns>Returns the new instance of ConnectorModel loaded from XmlElement.</returns> public static CSharpCommonGraph.Edge LoadConnectorsAndAddPortsFromXml(XmlElement connEl,IDictionary<string, CSharpCommonGraph.Node> nodes) { var helper = new Dynamo.Utilities.XmlElementHelper(connEl); var guid = helper.ReadGuid("guid", Guid.NewGuid()); var guidStart = helper.ReadGuid("start"); var guidEnd = helper.ReadGuid("end"); int startIndex = helper.ReadInteger("start_index"); int endIndex = helper.ReadInteger("end_index"); //find the elements to connect CSharpCommonGraph.Node start; if (nodes.TryGetValue(guidStart.ToString(), out start)) { var startport = new CSharpCommonGraph.Port(); startport.InstanceGuid = start.InstanceGuid.ToString()+"_OUT"+ startIndex.ToString(); startport.MetaData.Inspect = "start"; startport.Name = "a cool port"; //if we have not seen this port id before add the port, we dont want to duplicate ports if (start.Ports.All(x=>x.InstanceGuid != startport.InstanceGuid)) { start.Ports.Add(startport); } CSharpCommonGraph.Node end; if (nodes.TryGetValue(guidEnd.ToString(), out end)) { var endport = new CSharpCommonGraph.Port(); endport.InstanceGuid = end.InstanceGuid.ToString() + "_IN" + endIndex.ToString(); endport.MetaData.Inspect = "end"; endport.Name = "a cool port"; if (end.Ports.All(x => x.InstanceGuid != endport.InstanceGuid)) { end.Ports.Add(endport); } var edge = new CSharpCommonGraph.Edge(); edge.SrcGuid = startport.InstanceGuid; edge.DestGuid = endport.InstanceGuid; edge.InstanceGuid = guid.ToString(); return edge; } } return null; }
//Actual constructor for use in the program public static CommonGraph CommonGraphFromGHFile(string file) { CommonGraph cg = new CommonGraph(); //construct GH Archive object for XML Traversal GH_Archive archive = new GH_Archive(); archive.ReadFromFile(file); MetaData graphMetaData = new MetaData(); graphMetaData.Ignore = archive.Serialize_Xml(); cg.MetaData = graphMetaData; //traverse GH file tree var rootNode = archive.GetRootNode; var definition = rootNode.FindChunk("Definition"); var defObjects = definition.FindChunk("DefinitionObjects"); int objCount = defObjects.GetInt32("ObjectCount"); //for every object in the definition object list: for (int i = 0; i < objCount; i++) { var singleObjectChunk = defObjects.Chunks[i] as GH_Chunk; Guid typeGuid = singleObjectChunk.GetGuid("GUID"); var container = singleObjectChunk.FindChunk("Container"); var attributes = container.FindChunk("Attributes"); Guid instanceGuid = container.GetGuid("InstanceGuid"); string name = singleObjectChunk.GetString("Name"); IEnumerable<GH_IChunk> inputs; IEnumerable<GH_IChunk> outputs; //Components that implement variable parameters store their inputs/outputs one layer deeper. var parameterData = container.Chunks.Where<GH_IChunk>(C => C.Name == "ParameterData"); bool hasParameterData = parameterData.Count() > 0; bool hasSourceCount = container.ItemExists("SourceCount"); var paramChunks = container.Chunks; if (hasParameterData) { paramChunks = parameterData.ToList()[0].Chunks; inputs = paramChunks.Where(C => C.Name == "InputParam"); outputs = paramChunks.Where(C => C.Name == "OutputParam"); } else { inputs = paramChunks.Where(C => C.Name == "param_input"); outputs = paramChunks.Where(C => C.Name == "param_output"); } bool hasInputs = inputs.Count() > 0; bool hasOutputs = outputs.Count() > 0; bool isComponent = hasInputs || hasOutputs || hasParameterData; bool isActiveObject = isComponent || hasSourceCount; //Debugging //Console.WriteLine(name); //Console.WriteLine("Is active object? " + isActiveObject.ToString()); //Console.WriteLine("Is Component? " + isComponent.ToString()); if (!isActiveObject) continue; Node node = new Node(); //type and instance node.Type = typeGuid.ToString(); node.InstanceGuid = instanceGuid.ToString(); node.Name = name; Position pos = new Position(); try { var locPoint = attributes.GetDrawingPointF("Pivot"); pos.X = locPoint.X; pos.Y = locPoint.Y; } catch { } node.Position = pos; //Metadata MetaData md = new MetaData(); md.Ignore = chunkToXmlString(singleObjectChunk); //TODO - REMOVE COMPONENTS OF XML THAT SHOULDN'T BE INSPECTED md.Inspect = chunkToXmlString(singleObjectChunk); node.MetaData = md; List<Port> ports = new List<Port>(); List<Edge> edges = new List<Edge>(); if (isComponent) //if it's a component { List<GH_IChunk> portChunks = new List<GH_IChunk>(); portChunks.AddRange(inputs); portChunks.AddRange(outputs); foreach (var portIChunk in portChunks) // for every port "chunk" { Port port = new Port(); GH_Chunk portChunk = portIChunk as GH_Chunk; Guid portInstanceGuid = portChunk.GetGuid("InstanceGuid"); port.InstanceGuid = portInstanceGuid.ToString(); port.Name = portChunk.GetString("Name"); MetaData portMetadata = new MetaData(); portMetadata.Ignore = chunkToXmlString(portChunk); port.MetaData = portMetadata; //REMEMBER TO UNCOMMENT ports.Add(port); var sources = portChunk.Items.Where(item => item.Name == "Source"); //Console.WriteLine("WE GOT THIS MANY SOURCES:" +sources.Count()); foreach(GH_Item item in sources){ //Console.WriteLine("EDGE"); Edge edge = new Edge(); edge.DestGuid = portInstanceGuid.ToString(); edge.SrcGuid = item._guid.ToString(); edges.Add(edge); } } } else if(!isComponent && isActiveObject) //if it's a param { Port port = new Port(); //wrapper for object - if it's a param, instance for virtual node and port are the same. Guid portInstanceGuid = instanceGuid; port.InstanceGuid = instanceGuid.ToString(); port.Name = name; ports.Add(port); var sources = container.Items.Where(item => item.Name == "Source"); foreach (GH_Item source in sources) { Edge edge = new Edge(); edge.DestGuid = portInstanceGuid.ToString(); edge.SrcGuid = source._guid.ToString(); edges.Add(edge); } } node.Ports = ports; cg.Edges.AddRange(edges); cg.Nodes.Add(node); } return cg; }