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

For the Sketchplane code it won't be used in your case so you can ignore it.

 

For adding connector in the Family Editor I modified the code to match the updates you sent:

 

#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 importedElements = new FilteredElementCollector(CachedDoc).OfClass(typeof(ImportInstance));

                Element importedElement = importedElements.FirstElement();
                AddConnectorToFamily(importedElement);

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

        public void AddConnectorToFamily(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("InsertConnector");

                while (geoObjectItor.MoveNext())
                {
                    GeometryInstance symInst = geoObjectItor.Current as GeometryInstance;
                    GeometryElement symEle = symInst.SymbolGeometry;

                    foreach (GeometryObject symObj in symEle)
                    {
                        Solid theSolid = symObj as Solid;

                        if (null != theSolid)
                        {
                            foreach (Face face in theSolid.Faces)
                            {
                                PlanarFace pf = face as PlanarFace;

                                if (pf != null)
                                {
                                    XYZ connOrigin = new XYZ(-24.022405238, -59.669659519, 9.583333333);

                                    if (pf.Origin.IsAlmostEqualTo(connOrigin))
                                    {
                                        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));
                                            st.Commit();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                trans.Commit();
            }
        }
        #endregion
    }
}

You may need to replace the FaceNormal property with Normal property to match the Revit 2014 API.

Please notify me if everything is fine.


Viewing all articles
Browse latest Browse all 66666

Trending Articles



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