internal void NativeToSpeckleBase()
        {
            ConverterRevit kit = new ConverterRevit();

            kit.SetContextDocument(fixture.SourceDoc);

            foreach (var elem in fixture.RevitElements)
            {
                var spkElem = kit.ConvertToSpeckle(elem);
                Assert.NotNull(spkElem);
            }
            Assert.Empty(kit.ConversionErrors);
        }
        internal void NativeToSpeckle()
        {
            ConverterRevit converter = new ConverterRevit();

            converter.SetContextDocument(fixture.SourceDoc);

            foreach (var elem in fixture.RevitElements)
            {
                var spkElem = converter.ConvertToSpeckle(elem);
                if (spkElem is Base re)
                {
                    AssertValidSpeckleElement(elem, re);
                }
            }
            Assert.Empty(converter.ConversionErrors);
        }
        internal void SelectionToNative <T>(Action <T, T> assert)
        {
            ConverterRevit converter = new ConverterRevit();

            converter.SetContextDocument(fixture.SourceDoc);
            var spkElems = fixture.Selection.Select(x => converter.ConvertToSpeckle(x) as Base).ToList();

            converter = new ConverterRevit();
            converter.SetContextDocument(fixture.NewDoc);
            var revitEls = new List <object>();
            var resEls   = new List <object>();

            xru.RunInTransaction(() =>
            {
                //revitEls = spkElems.Select(x => kit.ConvertToNative(x)).ToList();
                foreach (var el in spkElems)
                {
                    var res = converter.ConvertToNative(el);
                    if (res is List <ApplicationPlaceholderObject> apls)
                    {
                        resEls.AddRange(apls);
                    }
                    else
                    {
                        resEls.Add(el);
                    }
                }
            }, fixture.NewDoc).Wait();

            Assert.Empty(converter.ConversionErrors);

            for (var i = 0; i < revitEls.Count; i++)
            {
                var sourceElem  = (T)(object)fixture.RevitElements[i];
                var destElement = (T)((ApplicationPlaceholderObject)resEls[i]).NativeObject;

                assert(sourceElem, (T)destElement);
            }
        }
        /// <summary>
        /// Gets elements from the fixture SourceDoc
        /// Converts them to Speckle
        /// Creates a new Doc (or uses the open one if open!)
        /// Converts the speckle objects to Native
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="assert"></param>
        internal List <object> SpeckleToNative <T>(Action <T, T> assert, UpdateData ud = null)
        {
            Document        doc      = null;
            IList <Element> elements = null;
            List <ApplicationPlaceholderObject> appPlaceholders = null;

            if (ud == null)
            {
                doc      = fixture.SourceDoc;
                elements = fixture.RevitElements;
            }
            else
            {
                doc             = ud.Doc;
                elements        = ud.Elements;
                appPlaceholders = ud.AppPlaceholders;
            }


            ConverterRevit converter = new ConverterRevit();

            converter.SetContextDocument(doc);
            //setting context objects for nested routine
            converter.SetContextObjects(elements.Select(obj => new ApplicationPlaceholderObject {
                applicationId = obj.UniqueId
            }).ToList());
            var spkElems = elements.Select(x => converter.ConvertToSpeckle(x)).Where(x => x != null).ToList();

            converter = new ConverterRevit();
            converter.SetContextDocument(fixture.NewDoc);
            //setting context objects for update routine
            if (appPlaceholders != null)
            {
                converter.SetPreviousContextObjects(appPlaceholders);
            }

            converter.SetContextObjects(spkElems.Select(x => new ApplicationPlaceholderObject {
                applicationId = x.applicationId, NativeObject = x
            }).ToList());


            var resEls = new List <object>();
            //used to associate th nested Base objects with eh flat revit ones
            var flatSpkElems = new List <Base>();

            xru.RunInTransaction(() =>
            {
                foreach (var el in spkElems)
                {
                    object res = null;
                    try
                    {
                        res = converter.ConvertToNative(el);
                    }
                    catch (Exception e)
                    {
                        converter.ConversionErrors.Add(new Error(e.Message, e.StackTrace));
                    }

                    if (res is List <ApplicationPlaceholderObject> apls)
                    {
                        resEls.AddRange(apls);
                        flatSpkElems.Add(el);
                        if (el["elements"] != null)
                        {
                            flatSpkElems.AddRange(el["elements"] as List <Base>);
                        }
                    }
                    else
                    {
                        resEls.Add(res);
                        flatSpkElems.Add(el);
                    }
                }
            }, fixture.NewDoc).Wait();

            Assert.Empty(converter.ConversionErrors);

            for (var i = 0; i < spkElems.Count; i++)
            {
                var sourceElem  = (T)(object)elements.FirstOrDefault(x => x.UniqueId == flatSpkElems[i].applicationId);
                var destElement = (T)((ApplicationPlaceholderObject)resEls[i]).NativeObject;
                assert(sourceElem, destElement);
            }

            return(resEls);
        }
示例#5
0
        public Base ConvertToSpeckle(object @object)
        {
            switch (@object)
            {
            case DS.Point o:
                return(PointToSpeckle(o));

            case DS.Vector o:
                return(VectorToSpeckle(o));

            case DS.Plane o:
                return(PlaneToSpeckle(o));

            case DS.Line o:
                return(LineToSpeckle(o));

            case DS.Rectangle o:
                return(PolylineToSpeckle(o));

            case DS.Polygon o:
                return(PolylineToSpeckle(o));

            case DS.Circle o:
                return(CircleToSpeckle(o));

            case DS.Arc o:
                return(ArcToSpeckle(o));

            case DS.Ellipse o:
                return(EllipseToSpeckle(o));

            case DS.EllipseArc o:
                return(EllipseToSpeckle(o));

            case DS.PolyCurve o:
                return(PolycurveToSpeckle(o));

            case DS.NurbsCurve o:
                return(CurveToSpeckle(o));

            case DS.Helix o:
                return(HelixToSpeckle(o));

            case DS.Curve o: //last of the curves
                return(CurveToSpeckle(o));

            case DS.Mesh o:
                return(MeshToSpeckle(o));

            case DS.Cuboid o:
                return(BoxToSpeckle(o));

#if REVIT
            //using the revit converter to handle Revit geometry
            case RD.Element o:
                var c = new ConverterRevit();
                c.SetContextDocument(Doc);
                return(c.ConvertToSpeckle(o.InternalElement));
#endif

            default:
                throw new NotSupportedException();
            }
        }