public override Value Evaluate(FSharpList <Value> args) { _points = ((Value.List)args[0]).Item; //point list _curves = ((Value.List)args[1]).Item; //spring list _d = ((Value.Number)args[2]).Item; //dampening _s = ((Value.Number)args[3]).Item; //spring constant _r = ((Value.Number)args[4]).Item; //rest length _use_rl = Convert.ToBoolean(((Value.Number)args[5]).Item); //use rest length _rlf = ((Value.Number)args[6]).Item; //rest length factor _m = ((Value.Number)args[7]).Item; //nodal mass _g = ((Value.Number)args[8]).Item; //gravity z component _threshold = ((Value.Number)args[9]).Item; //convergence threshold //if we are in the evaluate, this has been //marked dirty and we should set it to unconverged //in case one of the inputs has changed. particleSystem.setConverged(false); particleSystem.setGravity(_g); particleSystem.setThreshold(_threshold); //if the particle system has a different layout, then //clear it instead of updating if (particleSystem.numberOfParticles() == 0 || _fixPtCount != _points.Count() || _curves.Count() != particleSystem.numberOfSprings() || _reset) { ResetSystem(_points, _curves); } else { UpdateSystem(); } FSharpList <Value> forces = FSharpList <Value> .Empty; for (int i = 0; i < particleSystem.numberOfSprings(); i++) { forces = FSharpList <Value> .Cons(Value.NewNumber(particleSystem.getSpring(i).getResidualForce()), forces); } forces.Reverse(); FSharpList <Value> results = FSharpList <Value> .Empty; results = FSharpList <Value> .Cons(Value.NewList(forces), results); results = FSharpList <Value> .Cons(Value.NewContainer(particleSystem), results); //return Value.NewContainer(particleSystem); return(Value.NewList(results)); }
/// <summary> /// /// </summary> /// <param name="delay"></param> /// <param name="hand">Lunch & Learn: Note type: must be marshalled to list in C#</param> /// <param name="dealer"></param> private void UpdateHand(int delay, FSharpList <Card> hand, bool dealer) { var left = 0; var handReversed = hand.Reverse <Card>().ToList(); // new cards are added to head of list in F# so we must reverse them here. for (int i = 0; i < handReversed.Count; i++) { AddCardToDisplay(handReversed[i], left, dealer); left += CARD_OVERLAP; // pause before last card (usually the new card to show) if (i == handReversed.Count - 1) { Thread.Sleep(delay); } } }
public override Value Evaluate(FSharpList <Value> args) { var instance = (FamilyInstance)((Value.Container)args[0]).Item; // ADAPTIVE COMPONENT if (AdaptiveComponentInstanceUtils.IsAdaptiveComponentInstance(instance)) { var refPtIds = AdaptiveComponentInstanceUtils.GetInstancePlacementPointElementRefIds(instance); FSharpList <Value> refPts = FSharpList <Value> .Empty; foreach (var id in refPtIds) { var pt = dynRevitSettings.Doc.Document.GetElement(id) as ReferencePoint; refPts = FSharpList <Value> .Cons(Value.NewContainer(pt.Position), refPts); } return(Value.NewList(Utils.SequenceToFSharpList(refPts.Reverse()))); } // INSTANCE WITH PLACEMENT POINT var ptRefs = instance.GetFamilyPointPlacementReferences(); if (ptRefs.Any()) { var pts = ptRefs.Select(x => x.Location.Origin); var containers = pts.Select(Value.NewContainer); return(Value.NewList(Utils.SequenceToFSharpList(containers))); } LocationPoint point = null; LocationCurve c = null; // INSTANCE WITH LOCATION POINT point = instance.Location as LocationPoint; if (point != null) { return(Value.NewContainer(point.Point)); } //INSTANCE WITH LOCATION CURVE c = instance.Location as LocationCurve; if (c != null) { return(Value.NewContainer(c.Curve)); } throw new Exception("A location could not be found for the selected family instance(s)."); }
public override Value Evaluate(FSharpList <Value> args) { var symbol = (FamilySymbol)((Value.Container)args[0]).Item; var curves = ((Value.List)args[1]).Item; var targets = ((Value.List)args[2]).Item; if (curves.Count() != targets.Count()) { throw new Exception("The number of curves and the number of up vectors must be the same."); } var data = curves.Zip(targets, (first, second) => new Tuple <Curve, XYZ>((Curve)((Value.Container)first).Item, (XYZ)((Value.Container)second).Item)); var instData = new List <FamilyInstanceCreationData>(); int count = 0; foreach (var pair in data) { var curve = pair.Item1; var target = pair.Item2; //calculate the desired rotation //we do this by finding the angle between the z axis //and vector between the start of the beam and the target point //both projected onto the start plane of the beam. XYZ zAxis = new XYZ(0, 0, 1); XYZ yAxis = new XYZ(0, 1, 0); //flatten the beam line onto the XZ plane //using the start's z coordinate XYZ start = curve.get_EndPoint(0); XYZ end = curve.get_EndPoint(1); XYZ newEnd = new XYZ(end.X, end.Y, start.Z); //drop end point to plane ////use the x axis of the curve's transform ////as the normal of the start plane //XYZ planeNormal = (curve.get_EndPoint(0) - curve.get_EndPoint(1)).Normalize(); //catch the case where the end is directly above //the start, creating a normal with zero length //in that case, use the Z axis XYZ planeNormal = newEnd.IsAlmostEqualTo(start) ? zAxis : (newEnd - start).Normalize(); XYZ target_project = target - target.DotProduct(planeNormal) * planeNormal; XYZ z_project = zAxis - zAxis.DotProduct(planeNormal) * planeNormal; //double gamma = target_project.AngleTo(z_project); double gamma = target.AngleOnPlaneTo(zAxis.IsAlmostEqualTo(planeNormal) ? yAxis : zAxis, planeNormal); FamilyInstance instance = null; if (this.Elements.Count > count) { if (dynUtils.TryGetElement(this.Elements[count], out instance)) { if (instance.Symbol != symbol) { instance.Symbol = symbol; } //update the curve var locCurve = instance.Location as LocationCurve; locCurve.Curve = curve; } else { var beamData = new FamilyInstanceCreationData(curve, symbol, dynRevitSettings.DefaultLevel, StructuralType.Beam) { RotateAngle = gamma }; instData.Add(beamData); } } else { var beamData = new FamilyInstanceCreationData(curve, symbol, dynRevitSettings.DefaultLevel, StructuralType.Beam) { RotateAngle = gamma }; instData.Add(beamData); } count++; } //trim the elements collection foreach (var e in this.Elements.Skip(count)) { this.DeleteElement(e); } FSharpList <Value> results = FSharpList <Value> .Empty; if (instData.Any()) { var ids = dynRevitSettings.Doc.Document.Create.NewFamilyInstances2(instData); //add our batch-created instances ids' //to the elements collection ids.ToList().ForEach(x => Elements.Add(x)); } //add all of the instances results = Elements.Aggregate(results, (current, id) => FSharpList <Value> .Cons(Value.NewContainer(dynRevitSettings.Doc.Document.GetElement(id)), current)); results.Reverse(); return(Value.NewList(results)); }
public override Value Evaluate(FSharpList <Value> args) { var xyzs = ((Value.List)args[0]).Item; var symbol = (FamilySymbol)((Value.Container)args[1]).Item; var instData = new List <FamilyInstanceCreationData>(); var updated = new HashSet <ElementId>(); int count = 0; #region batch creation data var sw = new Stopwatch(); sw.Start(); foreach (var pts in xyzs) { FamilyInstance instance = null; if (Elements.Count > count) { if (dynUtils.TryGetElement(this.Elements[count], out instance)) { //if we've found an instance, change its symbol if it needs //changing if (instance.Symbol.Id != symbol.Id) { instance.Symbol = symbol; } //update the placement points and add the //id to the list of updated acs UpdatePlacementPoints(instance, xyzs, count); updated.Add(Elements[count]); } else { var instanceData = new FamilyInstanceCreationData(XYZ.Zero, symbol, StructuralType.NonStructural); instData.Add(instanceData); } } else { var instanceData = new FamilyInstanceCreationData(XYZ.Zero, symbol, StructuralType.NonStructural); instData.Add(instanceData); } count++; } sw.Stop(); Debug.WriteLine(string.Format("{0} elapsed for updating existing or generating family creation data.", sw.Elapsed)); sw.Reset(); #endregion //trim the elements collection foreach (var e in this.Elements.Skip(count)) { DeleteElement(e); } FSharpList <Value> results = FSharpList <Value> .Empty; sw.Start(); ICollection <ElementId> ids = new List <ElementId>(); if (instData.Any()) { if (dynRevitSettings.Doc.Document.IsFamilyDocument) { ids = dynRevitSettings.Doc.Document.FamilyCreate.NewFamilyInstances2(instData); } else { ids = dynRevitSettings.Doc.Document.Create.NewFamilyInstances2(instData); } if (ids.Count > 0) { //hack to force regeneration. There should be a better way!! XYZ moveVec = new XYZ(0.0, 0.0, 0.0); ElementTransformUtils.MoveElement(this.UIDocument.Document, ids.ElementAt(0), moveVec); } //add our batch-created instances ids' //to the elements collection ids.ToList().ForEach(x => Elements.Add(x)); } sw.Stop(); Debug.WriteLine(string.Format("{0} elapsed for creating instances from creation data.", sw.Elapsed)); sw.Reset(); sw.Start(); //make sure the ids list and the XYZ sets list //have the same length if (count != xyzs.Count()) { foreach (var eId in ids) { DeleteElement(eId); } throw new Exception("There are more adaptive component instances than there are points to adjust."); } try { for (var j = 0; j < Elements.Count; j++) { if (updated.Contains(Elements[j])) { continue; } FamilyInstance ac; if (!dynUtils.TryGetElement(Elements[j], out ac)) { continue; } UpdatePlacementPoints(ac, xyzs, j); } } catch (Exception ex) { foreach (var eId in ids) { DeleteElement(eId); } throw ex; } sw.Stop(); Debug.WriteLine(string.Format("{0} elapsed for updating remaining instance locations.", sw.Elapsed)); //add all of the instances results = Elements.Aggregate(results, (current, id) => FSharpList <Value> .Cons(Value.NewContainer(dynRevitSettings.Doc.Document.GetElement(id)), current)); results.Reverse(); return(Value.NewList(results)); }
public override FScheme.Value Evaluate(FSharpList <FScheme.Value> args) { Autodesk.Revit.DB.CurveLoop cl = (Autodesk.Revit.DB.CurveLoop)((FScheme.Value.Container)args[0]).Item; if (cl == null) { throw new InvalidOperationException("No curve loop"); } Autodesk.Revit.DB.Plane plane = cl.GetPlane(); if (plane == null) { throw new InvalidOperationException("Curve loop is not planar"); } var mcs = new System.Collections.Generic.List <Autodesk.Revit.DB.ModelCurve>(); var listCurves = new System.Collections.Generic.List <Curve> (); CurveLoopIterator CLiter = cl.GetCurveLoopIterator(); for (; CLiter.MoveNext();) { listCurves.Add(CLiter.Current.Clone()); } int numCurves = listCurves.Count; Autodesk.Revit.DB.SketchPlane sp = null; for (int index = 0; index < numCurves; index++) { //instead of changing Revit curve keep it "as is" //user might have trouble modifying curve in Revit if it is off the sketch plane Autodesk.Revit.DB.ModelCurve mc = null; if (this.Elements.Any() && index < this.Elements.Count) { bool needsRemake = false; if (dynUtils.TryGetElement(this.Elements[index], out mc)) { ElementId idSpUnused = ModelCurve.resetSketchPlaneMethod(mc, listCurves[index], plane, out needsRemake); if (idSpUnused != ElementId.InvalidElementId && index == numCurves - 1) { this.DeleteElement(idSpUnused); } if (!needsRemake) { if (!mc.GeometryCurve.IsBound && listCurves[index].IsBound) { listCurves[index] = listCurves[index].Clone(); listCurves[index].MakeUnbound(); } ModelCurve.setCurveMethod(mc, listCurves[index]); // mc.GeometryCurve = c; } else { this.DeleteElement(this.Elements[index]); } } else { needsRemake = true; } if (needsRemake) { if (sp == null) { sp = dynRevitSettings.Doc.Document.IsFamilyDocument ? dynRevitSettings.Doc.Document.FamilyCreate.NewSketchPlane(plane) : dynRevitSettings.Doc.Document.Create.NewSketchPlane(plane); } if (dynRevitUtils.GetPlaneFromCurve(listCurves[index], true) == null) { mc = this.UIDocument.Document.IsFamilyDocument ? this.UIDocument.Document.FamilyCreate.NewModelCurve(listCurves[index], sp) : this.UIDocument.Document.Create.NewModelCurve(listCurves[index], sp); ModelCurve.setCurveMethod(mc, listCurves[index]); } else { mc = this.UIDocument.Document.IsFamilyDocument ? this.UIDocument.Document.FamilyCreate.NewModelCurve(listCurves[index], sp) : this.UIDocument.Document.Create.NewModelCurve(listCurves[index], sp); } if (index < this.Elements.Count) { this.Elements[index] = mc.Id; } else { this.Elements.Add(mc.Id); } if (mc.SketchPlane.Id != sp.Id && index == numCurves - 1) { //THIS BIZARRE as Revit could use different existing SP, so if Revit had found better plane this sketch plane has no use this.DeleteElement(sp.Id); } } } else { if (sp == null) { sp = dynRevitSettings.Doc.Document.IsFamilyDocument ? dynRevitSettings.Doc.Document.FamilyCreate.NewSketchPlane(plane) : dynRevitSettings.Doc.Document.Create.NewSketchPlane(plane); } if (dynRevitUtils.GetPlaneFromCurve(listCurves[index], true) == null) { mc = this.UIDocument.Document.IsFamilyDocument ? this.UIDocument.Document.FamilyCreate.NewModelCurve(listCurves[index], sp) : this.UIDocument.Document.Create.NewModelCurve(listCurves[index], sp); ModelCurve.setCurveMethod(mc, listCurves[index]); } else { mc = this.UIDocument.Document.IsFamilyDocument ? this.UIDocument.Document.FamilyCreate.NewModelCurve(listCurves[index], sp) : this.UIDocument.Document.Create.NewModelCurve(listCurves[index], sp); } this.Elements.Add(mc.Id); if (mc.SketchPlane.Id != sp.Id && index == numCurves - 1) { //found better plane this.DeleteElement(sp.Id); } } if (mc != null) { mcs.Add(mc); } } FSharpList <FScheme.Value> results = FSharpList <FScheme.Value> .Empty; foreach (var mc in mcs) { results = FSharpList <FScheme.Value> .Cons(FScheme.Value.NewContainer(mc), results); } return(FScheme.Value.NewList(Utils.SequenceToFSharpList(results.Reverse()))); }