Example #1
0
        public void ProcessMultipleSAT()
        {
            List <ElementId> importIds = new List <ElementId>();

            // create and set new SAT options
            SATImportOptions satOptions = new SATImportOptions();

            satOptions.Placement = ImportPlacement.Origin;
            satOptions.ColorMode = ImportColorMode.BlackAndWhite;
            satOptions.Unit      = ImportUnit.Millimeter;

            using (Transaction trans = new Transaction(m_doc, "UpdateSAT"))
            {
                trans.Start();

                List <GeometryObject> geoObjList = new List <GeometryObject>();
                DirectShape           ds         = null;
                ElementId             currentId;

                try
                {
                    currentId = m_doc.Import(@"B:\Rhino\OpenNURBS\v5_example_file.sat", satOptions, m_doc.ActiveView);
                    importIds.Add(currentId);
                }
                catch (Exception)
                {
                    currentId = ElementId.InvalidElementId;
                }
                // extract geometry from import instance
                ImportInstance ii       = m_doc.GetElement(currentId) as ImportInstance;
                Options        gOptions = new Options();
                gOptions.ComputeReferences = true;
                GeometryElement geoElement = ii.get_Geometry(gOptions);

                // get solids from geometry element
                List <GeometryObject> tempGeoList = FindElementGeometry(geoElement);
                foreach (GeometryObject go in tempGeoList)
                {
                    geoObjList.Add(go);
                }


                ds = DirectShape.CreateElement(m_doc, new ElementId((int)BuiltInCategory.OST_GenericModel));
                ds.SetShape(geoObjList);

                // set the Direct Shape options
                DirectShapeOptions dsOptions = ds.GetOptions();
                dsOptions.ReferencingOption = DirectShapeReferencingOption.Referenceable;
                ds.SetOptions(dsOptions);

                trans.Commit();

                trans.Start("Delete Elements");
                // clean up imported solids
                m_doc.Delete(importIds);
                trans.Commit();
            }
        }
		public static void CreateOnViewDrafting(ViewDrafting view, ElementId linkId)
		{
			Document doc = view.Document;

			ImportInstance instance = doc.GetElement(linkId) as ImportInstance;
			GeometryElement geometryElement = instance.get_Geometry(new Options());
			foreach (GeometryObject go in geometryElement)
			{
				GeometryInstance ginstance = go as GeometryInstance;
				if (null != ginstance)
				{
					CreatePrimitives(view, ginstance.SymbolGeometry);
				}
			}
		}
Example #3
0
        /// <summary>
        /// Try To Get The Text From CAD But Failure
        /// </summary>
        /// <param name="revitDoc"></param>
        private void GetCADTest(Document revitDoc)
        {
            Document doc         = uidoc.Document;
            View     active_view = doc.ActiveView;
            List <GeometryObject> visible_dwg_geo = new List <GeometryObject>();

            // Pick Import Instance
            Reference                r   = uidoc.Selection.PickObject(ObjectType.Element, new JtElementsOfClassSelectionFilter <ImportInstance>());
            ImportInstance           dwg = doc.GetElement(r) as ImportInstance;
            FilteredElementCollector col = new FilteredElementCollector(doc).OfClass(typeof(ImportInstance));


            foreach (GeometryObject geometryObj in dwg.get_Geometry(new Options()))
            {
                if (geometryObj is GeometryInstance) // This will be the whole thing
                {
                    GeometryInstance dwgInstance = geometryObj as GeometryInstance;
                    var test = dwgInstance.Symbol;
                    foreach (GeometryObject blockObject in dwgInstance.SymbolGeometry)
                    {
                        if (blockObject is GeometryInstance) // This could be a block
                        {
                            //get the object name and coordinates and rotation and
                            //load into my own class
                            GeometryInstance blockInstance = blockObject as GeometryInstance;

                            string name = blockInstance.Symbol.Name;

                            Transform transform = blockInstance.Transform;

                            XYZ origin = transform.Origin;

                            XYZ    vectorTran = transform.OfVector(transform.BasisX.Normalize());
                            double rot        = transform.BasisX.AngleOnPlaneTo(vectorTran, transform.BasisZ.Normalize()); // radians
                            rot = rot * (180 / Math.PI);                                                                   // degrees
                        }
                    }
                }
            }
        }
Example #4
0
        private List <GeometryObject> GetLinkedDWGCurves(ImportInstance currentDWG)
        {
            List <GeometryObject> curvelist = new List <GeometryObject>();
            Options          curOptions     = new Options();
            GeometryElement  geoElement;
            GeometryElement  geoElement2;
            GeometryInstance geoInstance;

            geoElement = currentDWG.get_Geometry(curOptions);

            foreach (GeometryObject geoObject in geoElement)
            {
                //convert geoObject to geoInstance
                geoInstance = geoObject as GeometryInstance;
                geoElement2 = geoInstance.GetInstanceGeometry();

                foreach (GeometryObject curObject in geoElement2)
                {
                    curvelist.Add(curObject);
                }
            }
            return(curvelist);
        }
Example #5
0
        /// <summary>
        /// Pick a DWG import/linked instance (ver.2010 or below), extract all visible elements or
        /// ones within specific Layer(LineType) if the type(GeometryObjectType) is assigned.
        /// </summary>
        public static List <GeometryObject> ExtractElement(UIDocument uidoc, ImportInstance import, string layer = "*", string type = "*")
        {
            Document doc         = uidoc.Document;
            View     active_view = doc.ActiveView;

            List <GeometryObject> visible_dwg_geo = new List <GeometryObject>();

            // Get Geometry
            var geoElem = import.get_Geometry(new Options());

            Debug.Print("Found elements altogether: " + geoElem.Count().ToString());
            foreach (var geoObj in geoElem)
            {
                if (geoObj is GeometryInstance)
                {
                    var geoIns = geoObj as GeometryInstance;

                    // This may contain child GeometryInstance, so...
                    // If fully explosion is need, recrusive function is needed here
                    var ge2 = geoIns.GetInstanceGeometry();
                    if (ge2 != null)
                    {
                        foreach (var obj in ge2)
                        {
                            // Use the GraphicsStyle to get the DWG layer linked to the Category for visibility.
                            var gStyle = doc.GetElement(obj.GraphicsStyleId) as GraphicsStyle;

                            // If an object does not have a GraphicsStyle just skip it
                            if (gStyle == null)
                            {
                                continue;
                            }
                            //Debug.Print(obj.GetType().Name);

                            // Check if the layer is visible in the view.
                            if (!active_view.GetCategoryHidden(gStyle.GraphicsStyleCategory.Id))
                            {
                                if (layer == "*")
                                {
                                    if (type == "*")
                                    {
                                        visible_dwg_geo.Add(obj);
                                    }
                                    else if (obj.GetType().Name == type)
                                    {
                                        visible_dwg_geo.Add(obj);
                                    }
                                }
                                // Select a certain Linetype(Layername/StyleCategory)
                                else if (gStyle.GraphicsStyleCategory.Name == layer)
                                {
                                    if (type == "*")
                                    {
                                        visible_dwg_geo.Add(obj);
                                    }
                                    else if (obj.GetType().Name == type)
                                    {
                                        visible_dwg_geo.Add(obj);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Debug.Print("Geometry collected: " + visible_dwg_geo.Count().ToString());
            return(visible_dwg_geo);
        }
Example #6
0
        internal static IList <XYZ> GetDwgGeometryMidPoints(DwgGeometryType targetGeometryType, ImportInstance targetImportedInstance, string targetLayerName)
        {
            GeometryElement geoElem = targetImportedInstance.get_Geometry(new Options()
            {
                DetailLevel = ViewDetailLevel.Coarse, ComputeReferences = false, IncludeNonVisibleObjects = true
            });
            IList <XYZ> midPoints = new List <XYZ>();

            foreach (GeometryObject geoObj in geoElem)
            {
                if (geoObj is GeometryInstance)
                {
                    GeometryInstance geoInstance = geoObj as GeometryInstance;
                    GeometryElement  geo         = geoInstance.GetInstanceGeometry();

                    foreach (var currentGeo in geo)
                    {
                        #region if Poly Line
                        if ((currentGeo is PolyLine) && (targetGeometryType == DwgGeometryType.Rectangle))
                        {
                            PolyLine currentPolyLine = currentGeo as PolyLine;
                            int      numCoordinates  = currentPolyLine.NumberOfCoordinates;

                            if (numCoordinates == 5)
                            {
                                if (targetLayerName != null)
                                {
                                    if ((((targetImportedInstance.Document.GetElement(currentPolyLine.GraphicsStyleId) as GraphicsStyle).GraphicsStyleCategory).Name) != targetLayerName)
                                    {
                                        continue;
                                    }
                                }

                                IList <XYZ> coordinates = currentPolyLine.GetCoordinates();

                                IList <XYZ> first3Points = new List <XYZ>();
                                for (int i = 0; i < 3; i++)
                                {
                                    first3Points.Add(coordinates.ElementAt(i));
                                }

                                double maxDist = double.NegativeInfinity;
                                XYZ    point1  = new XYZ();
                                XYZ    point2  = new XYZ();

                                foreach (XYZ currentPoint1 in first3Points)
                                {
                                    foreach (XYZ currentPoint2 in first3Points)
                                    {
                                        double currentDist = currentPoint1.DistanceTo(currentPoint2);
                                        if (currentDist > maxDist)
                                        {
                                            maxDist = currentDist;
                                            point1  = currentPoint1;
                                            point2  = currentPoint2;
                                        }
                                    }
                                }

                                XYZ currentMidPoint = Utils.GetPoint.getMidPoint(point1, point2);
                                //We need to check if there`s point already near this one (or in the same place)
                                //If so, we cant create a duplicate point or a point almost equal to another
                                bool theresPointNear = false;
                                foreach (XYZ currentPoint in midPoints)
                                {
                                    if (currentMidPoint.DistanceTo(currentPoint) < 0.3)
                                    {
                                        theresPointNear = true;
                                    }
                                }
                                if (theresPointNear == false)
                                {
                                    midPoints.Add(currentMidPoint);
                                }
                            }
                        }
                        #endregion

                        #region if Arc - Circle
                        if ((currentGeo is Arc) && (targetGeometryType == DwgGeometryType.Circle))
                        {
                            Arc currentArc = currentGeo as Arc;

                            if ((currentArc.IsCyclic == false) || (currentArc.IsBound == true))
                            {
                                continue;
                            }

                            if (targetLayerName != null)
                            {
                                if ((((targetImportedInstance.Document.GetElement(currentArc.GraphicsStyleId) as GraphicsStyle).GraphicsStyleCategory).Name) != targetLayerName)
                                {
                                    continue;
                                }
                            }

                            XYZ currentMidPoint = currentArc.Center;
                            //We need to check if there`s point already near this one (or in the same place)
                            //If so, we cant create a duplicate point or a point almost equal to another
                            bool theresPointNear = false;
                            foreach (XYZ currentPoint in midPoints)
                            {
                                if (currentMidPoint.DistanceTo(currentPoint) < 0.3)
                                {
                                    theresPointNear = true;
                                }
                            }
                            if (theresPointNear == false)
                            {
                                midPoints.Add(currentMidPoint);
                            }
                        }
                    }
                    #endregion
                }
            }
            return(midPoints);
        }