/yeti/model/graph/nodes/geometry/util/customObject.cs
C# | 391 lines | 195 code | 72 blank | 124 comment | 17 complexity | 65d552a3f1929dfeb4a9269fcbf21c9d MD5 | raw file
Possible License(s): GPL-3.0
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- using System.Diagnostics;
- using System.Collections;
-
- using RMA.OpenNURBS;
-
- using yeti.model.yaml;
- using yeti.view.rhinoStuff;
-
- namespace yeti.model.graph.nodes
- {
- /// <summary>
- /// The customObject node has two functions:
- /// 1. It keeps track of all the custom Objects that have been created
- /// - The names of the custom objects
- /// - The parameters to the custom objects
- /// - The tokens that represent the custom objects.
- ///
- /// 2. It acts like a node in the graph
- ///
- /// So if you are to make:
- ///
- /// pointRow:
- /// yPos: {1,2,4}
- ///
- /// --- !pointRow
- /// point:
- /// y: !yPos
- /// x:
- /// from: 0
- /// to: 20
- /// ...
- ///
- /// Then all the code for pointRow is compiled into a graph.
- /// The customObject:
- /// - Keeps track of that graph (or at least what type of graph the customObject should be)
- /// - Setting the parameters of the custom object set nodes in the graph.
- /// - On draw all the parameters are applied and graph is drawn.
- ///
- ///
- /// Note that parameters are stored in a hashtable and the graph is stored just as a type.
- /// Rather than creating and applying the parameters to the graph when they come in,
- /// the graph is only created and the parameters only applied, when the graph is needed.
- /// </summary>
- class customObject : INodeData
- {
-
- ///////////////////////////////////////////
- // LANGUAGE
- ///////////////////////////////////////////
-
- public static readonly nodeLanguage language = new nodeLanguage
- {
- isPublic = false,
- isArray = false,
- Keyword = "",
- icon = yeti.view.dialog.textarea.iconType.myObject,
- Parameters = new Dictionary<string, Type>()
- {
- },
- Constructor = new string[] {}
- };
-
- public override nodeLanguage getLanguage()
- {
- return language;
- }
-
- public override string ToString()
- {
- return myType;
- }
-
-
-
- ///////////////////////////////////////////
- // CUSTOM OBJECT LANGUAGE
- ///////////////////////////////////////////
-
- /// <summary>
- /// TODO: The customObjects should probably be stored with the graph...
- /// </summary>
- private static Dictionary<string, lexical> customObjects = new Dictionary<string, lexical>();
- private static Dictionary<string, parametricGraph> customObjectsGraph = new Dictionary<string, parametricGraph>();
-
-
- ///
- /// <summary>
- /// This is what is created when a new object is selected in intellisense.
- /// </summary>
- /// <returns></returns>
- public static string getObjectDefault()
- {
- return @"--- !unnamed
- ...";
- }
-
- /// <summary>
- /// Removes all the objects from the language and starts again.
- /// </summary>
- public static void resetObjects()
- {
- customObjects = new Dictionary<string, lexical>();
- customObjectsGraph = new Dictionary<string, parametricGraph>();
-
- }
-
- /// <summary>
- /// Adds an object to the language
- /// </summary>
- /// <param name="objectName">Name of object</param>
- /// <param name="lexicalItRepresents">Lexical of the object.</param>
- public static void addObject(string objectName, lexical lexicalItRepresents)
- {
- if (objectName == null) return;
-
- customObjects.Add(objectName, lexicalItRepresents);
- }
-
-
-
- /// <summary>
- /// Gets all the objects currently in the language.
- /// </summary>
- /// <returns></returns>
- public static string[] getObjects()
- {
- //TODO: Cache this, and order alphabetically.
- List<string> newObjectList = new List<string>(customObjects.Keys);
- return newObjectList.ToArray();
- }
-
- public static bool isTypeOfObject(string objectKeyword)
- {
- return customObjects.ContainsKey(objectKeyword);
- }
-
- public static bool isParameterToObject(string objectKeyword, string parameter)
- {
- if (customObjects.ContainsKey(objectKeyword))
- {
- return customObjects[objectKeyword].isParameter(parameter);
- }
- return false;
- }
-
- public static Dictionary<string, Type> getObjectParameters(string objectKeyword)
- {
- if (customObjects.ContainsKey(objectKeyword))
- {
- return customObjects[objectKeyword].getParameters();
- }
- return null;
- }
-
- /// <summary>
- /// Gets the graph associated with a particular objectKeyword
- /// Returns false if an objectKeyword does not exist.
- /// </summary>
- /// <param name="objectKeyword"></param>
- /// <returns></returns>
- public static parametricGraph getObjectGraph(string objectKeyword)
- {
- if (customObjects.ContainsKey(objectKeyword))
- {
- return customObjects[objectKeyword].getGraph();
-
- //TODO: Cache the graph here.
- //before this can be done, the graph needs to be reset...
- //something to do with the set values??
- /*
- if (!customObjectsGraph.ContainsKey(objectKeyword))
- {
- customObjectsGraph.Add(objectKeyword, customObjects[objectKeyword].getGraph());
- }
- else
- {
-
- customObjectsGraph[objectKeyword].reset();
- }
-
- return customObjectsGraph[objectKeyword];*/
- }
- return null;
- }
-
- public static Type getObjectParameterType(string objectKeyword, string parameter)
- {
- if (customObjects.ContainsKey(objectKeyword))
- {
- return customObjects[objectKeyword].getParameterType(parameter);
- }
- return null;
- }
-
-
- public static lexical getObjectLexical(string objectKeyword)
- {
- if (customObjects.ContainsKey(objectKeyword))
- {
- return customObjects[objectKeyword];
- }
- return null;
- }
-
-
-
-
-
-
- ///////////////////////////////////////////
- // DATA
- ///////////////////////////////////////////
-
- private string myType;
- Hashtable parameterHashtable = new Hashtable();
-
-
-
- ///////////////////////////////////////////
- // CONSTRUCTORS
- ///////////////////////////////////////////
-
- public customObject(string _myType)
- : this(_myType, new Hashtable())
- {
- }
-
- public customObject(string _myType, Hashtable _parameterHashtable)
- {
- myType = _myType;
- parameterHashtable = _parameterHashtable;
- }
-
- public override INodeData clone()
- {
- //NOTE: to clone the custom node, we just copy the type and the parameterHashtable
- //The graph is created on the fly when we need it.
- return new customObject(myType, (Hashtable) parameterHashtable.Clone());
- }
-
-
-
- public string getMyType()
- {
- return myType;
- }
-
-
- ///////////////////////////////////////////
- // PARAMETERS
- ///////////////////////////////////////////
-
-
- public override object value
- {
- get
- {
- return null;
- }
- set
- {
- }
- }
-
- /// <summary>
- /// Rather than creating and updating the graph here,
- /// parameters are sent to a hashtable, and then when the graph is ready to
- /// be updated, everything is applied.
- /// </summary>
- /// <param name="parameterName"></param>
- /// <param name="values"></param>
- public override void setParameter(string parameterName, object value)
- {
- parameterHashtable.Add(parameterName, value);
- }
-
- /// <summary>
- /// Extracts the data from a node in the graph that shares the parameterName.
- /// Need to solve the graph before extracting parameters from it.
- ///
- /// </summary>
- /// <param name="parameterName"></param>
- /// <returns></returns>
- public override object getParameter(string parameterName)
- {
- //TODO:
- //This is super inefficent to create the graph every time you want a parameter.
- //In future, once node is updated, cache the results of the Graph and delete.
- parametricGraph myGraph = customObjects[myType].getGraph();
- //parametricGraph myGraph = customObject.getObjectGraph(myType);
-
- if (myGraph == null) return null;
-
- foreach (DictionaryEntry entry in parameterHashtable)
- {
- string parameter = (string)entry.Key;
- object value = entry.Value;
- if (myGraph.hasNode(parameter))
- {
- myGraph.getNode(parameter).setValue(new object[] { value });
- }
- }
-
- myGraph.update();
-
- INodeData[] data = myGraph.getNode(parameterName).getData();
-
- List<object> dataObjects = new List<object>();
-
- foreach (INodeData datum in data)
- {
- object datumParamValue = datum.getParameter("value");
-
- if (datum.getLanguage().isArray)
- {
- object[] datumParamArray = (object[])datumParamValue;
- dataObjects.AddRange(datumParamArray);
- }
- else
- {
- dataObjects.Add(datumParamValue);
- }
- }
-
- return dataObjects.ToArray();
- }
-
-
-
-
-
- ///////////////////////////////////////////
- // OUTPUTS
- ///////////////////////////////////////////
-
- /// <summary>
- /// To draw this node we need to update the graph with the right parameters
- /// and then tell it to redraw.
- /// TODO: In future can this be cached??
- /// </summary>
- public override void draw(bool highlight)
- {
- parametricGraph myGraph = customObject.getObjectGraph(myType);
-
- if (myGraph == null) return;
-
- foreach (DictionaryEntry entry in parameterHashtable)
- {
- string parameter = (string)entry.Key;
- object value = entry.Value;
- if (myGraph.hasNode(parameter))
- {
- myGraph.getNode(parameter).setValue(new object[] { value });
- }
- }
-
- myGraph.update();
- myGraph.draw(highlight);
- }
-
- /// <summary>
- /// To bake this node we need to update the graph with the right parameters
- /// then we tell it to bake.
- /// </summary>
- /// <param name="nameOfObject"></param>
- /// <param name="docToBakeTo"></param>
- public override void bake(string nameOfObject, RMA.Rhino.MRhinoDoc docToBakeTo)
- {
- parametricGraph myGraph = customObject.getObjectGraph(myType);
-
- if (myGraph == null) return;
-
- foreach (DictionaryEntry entry in parameterHashtable)
- {
- string parameter = (string)entry.Key;
- object value = entry.Value;
- if (myGraph.hasNode(parameter))
- {
- myGraph.getNode(parameter).setValue(new object[] { value });
- }
- }
-
- myGraph.update();
- myGraph.bake(docToBakeTo);
- }
- }
- }