Inheritance: IShowPropertyImpl, ICommandHandler, ISerializable
Esempio n. 1
0
        private object GetValue(NamedValuesProperty namedValues, string typename, string formula)
        {
            if (Settings.GlobalSettings.GetBoolValue("Scripting.ForceFloat", false))
            {
                formula = Regex.Replace(formula, @"(?<=/)(\d+)\b(?!\.)", "$1.0"); // macht aus "1/2" "1/2.0"
            }
            string code = @"
				using System;
				using System.Collections;
				using CADability;
				public class ScriptClass
				{
					double sin(double d) { return Math.Sin(d); }
					double cos(double d) { return Math.Cos(d); }
					double tan(double d) { return Math.Tan(d); }
					double Sin(double d) { return Math.Sin(d/180*Math.PI); }
					double Cos(double d) { return Math.Cos(d/180*Math.PI); }
					double Tan(double d) { return Math.Tan(d/180*Math.PI); }
					GeoVector v(double x, double y, double z) { return new GeoVector(x,y,z); }
					GeoPoint p(double x, double y, double z) { return new GeoPoint(x,y,z); }
					%namedValues%
					Hashtable namedValues;
					public ScriptClass(Hashtable namedValues)
					{
						this.namedValues = namedValues;
					}
					public %type% Calculate()
					{
						return %formula%;
					}
				}
				"                ;

            code = code.Replace("%formula%", formula);
            code = code.Replace("%type%", typename);
            code = code.Replace("%namedValues%", namedValues.GetCode());
            CompilerResults results = compiler.CompileAssemblyFromSource(compilerParams, code);

            if (results.Errors.Count > 0)
            {
                throw new ScriptingException("CompileAssemblyFromSource error");
            }
            Assembly generatedAssembly = results.CompiledAssembly;

            try
            {
                Module[] mods  = generatedAssembly.GetModules(false);
                Type[]   types = mods[0].GetTypes();
                foreach (Type type in types)
                {
                    if (type.Name == "ScriptClass")
                    {
                        ConstructorInfo ci          = type.GetConstructor(new Type[] { typeof(Hashtable) });
                        object          scriptClass = ci.Invoke(new object[] { namedValues.Table });
                        MethodInfo      mi          = type.GetMethod("Calculate");
                        if (mi != null)
                        {
                            try
                            {
                                return(mi.Invoke(scriptClass, null));
                            }
                            catch (TargetInvocationException)
                            {
                                throw new ScriptingException("General error");
                            }
                        }
                    }
                }
            }
            catch (Exception e) // wenn hier irgendwas schief geht, dann nicht mehr asynchron laufen lassen
            {
                if (e is ThreadAbortException)
                {
                    throw (e);
                }
                throw new ScriptingException("General error");
            }
            throw new ScriptingException("General error");
        }
Esempio n. 2
0
 public GeoPoint GetGeoPoint(NamedValuesProperty namedValues, string formula)
 {
     return((GeoPoint)GetValue(namedValues, "GeoPoint", formula));
 }
Esempio n. 3
0
 public double GetDouble(NamedValuesProperty namedValues, string formula)
 {
     return((double)GetValue(namedValues, "double", formula));
 }
Esempio n. 4
0
 public GeoVector GetGeoVector(NamedValuesProperty namedValues, string formula)
 {
     return((GeoVector)GetValue(namedValues, "GeoVector", formula));
 }