protected override void DoAction() { string regionTag = ParseStringForVariable(RegionTag); IMathcadRegion2 region = Worksheet.GetRegionByTag(regionTag); if (region == null) { throw new ActionException(string.Format("No such region {0}", regionTag), this); } if (region.Type != MCRegionType.mcMathRegion) { throw new ActionException( string.Format("Found region matching {0}, but was not math region", regionTag), this); } McValueType type = Text ? McValueType.String : Complex ? McValueType.Complex : McValueType.Numeric; if (!String.IsNullOrEmpty(Units)) { type |= McValueType.Dimensioned; } SetValueInRegion(region, Value, type, Units); }
private void SetNumericValue(IMathcadRegion2 region, dynamic evaluated, McValueType type, string units) { var regionXml = new XmlDocument(); regionXml.LoadXml(region.MathInterface.XML); XmlElement valueElement; XmlElement realElement = regionXml.GetElementsByTagName(@"ml:real").OfType <XmlElement>().SingleOrDefault(); dynamic realpart; if (type.HasFlag(McValueType.NumericHasComplexPart)) { XmlElement imagElement = regionXml.GetElementsByTagName(@"ml:imag") .OfType <XmlElement>() .SingleOrDefault(); Complex value = evaluated; realpart = value.Real; if (imagElement != null) { imagElement.Value = value.Imaginary.ToString(CultureInfo.InvariantCulture); valueElement = (XmlElement)imagElement.ParentNode.ParentNode; } else { var parent = realElement.ParentNode; XmlElement complexElement = value.ToMathML(); parent.ReplaceChild(realElement, complexElement); valueElement = complexElement; } } else { realpart = evaluated.ToString(); valueElement = realElement; } realElement.Value = realpart; if (type.HasFlag(McValueType.Dimensioned)) { XmlElement[] idElements = regionXml.GetElementsByTagName(@"ml:id").OfType <XmlElement>().ToArray(); if (idElements.Count() > 1) { idElements.Last().Value = units; } else { valueElement.ParentNode.ReplaceChild(valueElement, this.ApplyUnits(valueElement, units)); } } }
protected override void DoAction() { string varName = ParseStringForVariable(MathcadVarName), varSubscr = ParseStringForVariable(MathcadVarSubscript); IMathcadRegion2 region = Worksheet.GetRegionByVar(varName, varSubscr); if (region == null) { throw new ActionException(string.Format("Could not find region for {0}_{1}", varName, varSubscr), this); } McValueType type = Text ? McValueType.String : Complex ? McValueType.Complex : McValueType.Numeric; if (!String.IsNullOrEmpty(Units)) { type |= McValueType.Dimensioned; } SetValueInRegion(region, Value, type, Units); }
private Variable GetVarNameInRegion(IMathcadRegion2 region) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(region.MathInterface.XML); XmlElement element; try { element = xmlDoc.GetElementsByTagName("ml:id").OfType <XmlElement>().First(); } catch (InvalidOperationException) { return(new Variable()); } string varName = element.Value; string subscript = element.GetAttribute(@"subscript"); return(new Variable(varName, !String.IsNullOrEmpty(subscript) ? subscript : null)); }
protected void SetValueInRegion(IMathcadRegion2 region, String value, McValueType type, string units) { Centipede.IPythonEngine engine = GetCurrentCore().PythonEngine; dynamic evaluated = engine.Evaluate(value); var regionXml = new XmlDocument(); regionXml.LoadXml(region.MathInterface.XML); if (!type.HasFlag(McValueType.Numeric)) { SetNumericValue(region, evaluated, type, ParseStringForVariable(units)); } else { XmlElement textElement = regionXml.GetElementsByTagName(@"ml:str").OfType <XmlElement>().SingleOrDefault(); textElement.Value = evaluated.ToString(); } region.MathInterface.XML = regionXml.InnerText; }