Quantcast
Channel: All Revit API Forum posts
Viewing all articles
Browse latest Browse all 66666

Re: Adding connectors to obejct exported from AutoCAD.

$
0
0

Here you are the complete code:

#region Namespaces

using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;

using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;

//using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;

using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;

#endregion

namespace RevitAddinCS2
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class ExtCmd : IExternalCommand
    {
        #region Cached Variables

        private static ExternalCommandData _cachedCmdData;

        public static UIApplication CachedUiApp
        {
            get
            {
                return _cachedCmdData.Application;
            }
        }

        public static RvtApplication CachedApp
        {
            get
            {
                return CachedUiApp.Application;
            }
        }

        public static RvtDocument CachedDoc
        {
            get
            {
                return CachedUiApp.ActiveUIDocument.Document;
            }
        }

        #endregion

        #region IExternalCommand Members

        public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
        {
            _cachedCmdData = cmdData;

            try
            {
                //TODO: add your code below.
                FilteredElementCollector importCAD = new FilteredElementCollector(CachedDoc).OfClass(typeof(FreeFormElement));

                Element modelText = importCAD.FirstElement();
                PaintModelTextFaces(modelText);

                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
                return Result.Failed;
            }
        }

        public void PaintModelTextFaces(Element element)
        {
            // Before acquiring the geometry, make sure the detail level is set to 'Fine'
            Options geoOptions = CachedDoc.Application.Create.NewGeometryOptions();
            geoOptions.DetailLevel = ViewDetailLevel.Fine;
            geoOptions.ComputeReferences = true;
            // Obtain geometry for the given Wall element
            GeometryElement geoElem = element.get_Geometry(geoOptions);

            using (Transaction trans = new Transaction(CachedDoc))
            {
                IEnumerator<GeometryObject> geoObjectItor = geoElem.GetEnumerator();

                trans.Start("PaintModelTextFaces");

                while (geoObjectItor.MoveNext())
                {
                    // need to find a solid first
                    Solid theSolid = geoObjectItor.Current as Solid;
                    if (null != theSolid)
                    {
                        foreach (Face face in theSolid.Faces)
                        {
                            PlanarFace pf = face as PlanarFace;

                            if (pf != null)
                            {
                                Reference mReference = null;

                                using (SubTransaction st = new SubTransaction(CachedDoc))
                                {
                                    st.Start();

                                    Plane plane = new Plane(pf.FaceNormal, pf.Origin);

                                    List<Face> faces = face.GetRegions().ToList();

                                    SketchPlane sp = SketchPlane.Create(CachedDoc, plane);

                                    CachedDoc.ActiveView.SketchPlane = sp;
                                    mReference = sp.GetPlaneReference();

                                    //CachedDoc.ActiveView.ShowActiveWorkPlane();
                                    st.Commit();
                                }

                                using (SubTransaction st = new SubTransaction(CachedDoc))
                                {
                                    st.Start();
                                    ConnectorElement ce = ConnectorElement.CreateDuctConnector(CachedDoc, DuctSystemType.SupplyAir, ConnectorProfileType.Round, pf.Reference,pf.EdgeLoops.get_Item(0).get_Item(0));
                                    //ConnectorManager cm = new ConnectorManager();
                                    //cm.Connectors.Insert(ce.co);
                                    st.Commit();
                                }
                            }
                        }
                    }
                }
                trans.Commit();
            }
        }
        #endregion
    }
}

It will place a connector on every face so you have to make your modification to select the faces you want.

If this code help please don't forget to mark this reply as an answer.


Viewing all articles
Browse latest Browse all 66666

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>