BuildingCoder.ScottWilsonVoodooMagic.GetSpecialFamilyReference C# (CSharp) Метод

GetSpecialFamilyReference() публичный статический Метод

public static GetSpecialFamilyReference ( FamilyInstance inst, SpecialReferenceType refType ) : System.Reference
inst FamilyInstance
refType SpecialReferenceType
Результат System.Reference
        public static Reference GetSpecialFamilyReference( 
            FamilyInstance inst,
            SpecialReferenceType refType)
        {
            Reference indexRef = null;

            int idx = (int) refType;

            if( inst != null )
            {
              Document dbDoc = inst.Document;

              Options geomOptions = dbDoc.Application.Create
            .NewGeometryOptions();

              if( geomOptions != null )
              {
            geomOptions.ComputeReferences = true;
            geomOptions.DetailLevel = ViewDetailLevel.Undefined;
            geomOptions.IncludeNonVisibleObjects = true;
              }

              GeometryElement gElement = inst.get_Geometry(
            geomOptions );

              GeometryInstance gInst = gElement.First()
            as GeometryInstance;

              String sampleStableRef = null;

              if( gInst != null )
              {
            GeometryElement gSymbol = gInst
              .GetSymbolGeometry();

            if( gSymbol != null )
            {
              foreach( GeometryObject geomObj in gSymbol )
              {
            if( geomObj is Solid )
            {
              Solid solid = geomObj as Solid;

              if( solid.Faces.Size > 0 )
              {
                Face face = solid.Faces.get_Item( 0 );

                sampleStableRef = face.Reference
                  .ConvertToStableRepresentation(
                    dbDoc );

                break;
              }
            }
            else if( geomObj is Curve )
            {
              Curve curve = geomObj as Curve;

              sampleStableRef = curve.Reference
                .ConvertToStableRepresentation( dbDoc );

              break;
            }
            else if( geomObj is Point )
            {
              Point point = geomObj as Point;

              sampleStableRef = point.Reference
                .ConvertToStableRepresentation( dbDoc );

              break;
            }
              }
            }

            if( sampleStableRef != null )
            {
              String[] refTokens = sampleStableRef.Split(
            new char[] { ':' } );

              String customStableRef = refTokens[0] + ":"
            + refTokens[1] + ":" + refTokens[2] + ":"
            + refTokens[3] + ":" + idx.ToString();

              indexRef = Reference
            .ParseFromStableRepresentation(
              dbDoc, customStableRef );

              GeometryObject geoObj = inst
            .GetGeometryObjectFromReference(
              indexRef );

              if( geoObj != null )
              {
            String finalToken = "";

            if( geoObj is Edge )
            {
              finalToken = ":LINEAR";
            }

            if( geoObj is Face )
            {
              finalToken = ":SURFACE";
            }

            customStableRef += finalToken;

            indexRef = Reference
              .ParseFromStableRepresentation(
                dbDoc, customStableRef );
              }
              else
              {
            indexRef = null;
              }
            }
              }
              else
              {
            throw new Exception( "No Symbol Geometry found..." );
              }
            }
            return indexRef;
        }

Usage Example

        public void test(UIDocument uidoc)
        {
            Document doc = uidoc.Document;

            ReferenceArray refs = new ReferenceArray();

            Reference myRef = uidoc.Selection.PickObject(
                ObjectType.Element,
                new MySelectionFilter("Walls"),
                "Select a wall");

            Wall wall = doc.GetElement(myRef) as Wall;

            // Creates an element e from the selected object
            // reference -- this will be the wall element
            Element e = doc.GetElement(myRef);

            // Creates a selection filter to dump objects
            // in for later selection
            ICollection <ElementId> selSet = new List <ElementId>();

            // Gets the bounding box of the selected wall
            // element picked above
            BoundingBoxXYZ bb = e.get_BoundingBox(doc.ActiveView);

            // adds a buffer to the bounding box to ensure
            // all elements are contained within the box
            XYZ buffer = new XYZ(0.1, 0.1, 0.1);

            // creates an ouline based on the boundingbox
            // corners of the panel and adds the buffer
            Outline outline = new Outline(
                bb.Min - buffer, bb.Max + buffer);

            // filters the selection by the bounding box of the selected object
            // the "true" statement inverts the selection and selects all other objects
            BoundingBoxIsInsideFilter bbfilter
                = new BoundingBoxIsInsideFilter(outline, false);

            ICollection <BuiltInCategory> bcat
                = new List <BuiltInCategory>();

            //creates a new filtered element collector that
            // filters by the active view settings
            FilteredElementCollector collector
                = new FilteredElementCollector(
                      doc, doc.ActiveView.Id);

            //collects all objects that pass through the
            // requirements of the bbfilter
            collector.WherePasses(bbfilter);

            //add all levels and grids to filter -- these
            // are filtered out by the viewtemplate, but
            // are nice to have
            bcat.Add(BuiltInCategory.OST_StructConnections);

            //create new multi category filter
            ElementMulticategoryFilter multiCatFilter
                = new ElementMulticategoryFilter(bcat);

            //create new filtered element collector, add the
            // passing levels and grids, then remove them
            // from the selection
            foreach (Element el in collector.WherePasses(
                         multiCatFilter))
            {
                if (el.Name.Equals("EMBEDS"))
                {
                    selSet.Add(el.Id);
                }
            }

            XYZ[] pts = new XYZ[99];

            //View3D view = doc.ActiveView as View3D;
            View3D view = Get3dView(doc);

            // THIS IS WHERE IT RETURNS THE WALL OPENING REFERENCES.
            // HOWEVER THEY ONLY ARE ABLE TO BE USED FOR DIMENSIONS
            // IF THE OPENING IS CREATED USING A FAMILY SUCH AS A
            // WINDOW OR DOOR. OPENING BY FACE/WALL DOES NOT WORK,
            // EVEN THOUGH IT RETURNS PROPER REFERENCES

            List <Reference> openings = GetWallOpenings(e as Wall, view);

            foreach (Reference reference in openings)
            {
                refs.Append(reference);
            }

            TaskDialog.Show("REFERE", refs.Size.ToString());

            Curve wallLocation = (wall.Location as LocationCurve).Curve;

            int i = 0;

            foreach (ElementId ele in selSet)
            {
                FamilyInstance fi = doc.GetElement(ele) as FamilyInstance;
                Reference      reference
                    = ScottWilsonVoodooMagic.GetSpecialFamilyReference(
                          fi, ScottWilsonVoodooMagic.SpecialReferenceType.CenterLR,
                          doc);

                refs.Append(reference);

                pts[i] = (fi.Location as LocationPoint).Point;
                i++;
            }

            XYZ offset = new XYZ(0, 0, 4);

            Line line = Line.CreateBound(
                pts[0] + offset, pts[1] + offset);

            using (Transaction t = new Transaction(doc))
            {
                t.Start("dimension embeds");

                Dimension dim = doc.Create.NewDimension(doc.ActiveView, line, refs);

                t.Commit();
            }
        }