Example #1
0
        public static Autodesk.Revit.UI.Result DoSend(ExternalCommandData commandData)
        {
            using (Transaction transaction = new Transaction(commandData.Application.ActiveUIDocument.Document))
            {
                if (transaction.Start("changeParameters") == TransactionStatus.Started)
                {
                    // ElementId activeOptId = Autodesk.Revit.DB.DesignOption.GetActiveDesignOptionId(commandData.Application.ActiveUIDocument.Document);

                    //ElementDesignOptionFilter filter = new ElementDesignOptionFilter(activeOptId);

                    COVER.Instance.View3D           = null;
                    COVER.Instance.LinkedFileName   = "";
                    COVER.Instance.CurrentLink      = null;
                    COVER.Instance.LinkedDocumentID = 0;
                    COVER.Instance.DocumentID       = 0;
                    COVER.Instance.documentList.Clear();

                    COVER.Instance.documentList.Add(commandData.Application.ActiveUIDocument.Document);
                    Autodesk.Revit.DB.FilteredElementCollector collector = new Autodesk.Revit.DB.FilteredElementCollector(commandData.Application.ActiveUIDocument.Document);
                    COVER.Instance.SendGeometry(collector./*WherePasses(filter).*/ WhereElementIsNotElementType().GetElementIterator(), commandData.Application.ActiveUIDocument, commandData.Application.ActiveUIDocument.Document);

                    ElementClassFilter       FamilyFilter    = new ElementClassFilter(typeof(FamilySymbol));
                    FilteredElementCollector FamilyCollector = new FilteredElementCollector(commandData.Application.ActiveUIDocument.Document);
                    ICollection <Element>    AllFamilies     = FamilyCollector.WherePasses(FamilyFilter).ToElements();
                    foreach (FamilySymbol Fmly in AllFamilies)
                    {
                        COVER.Instance.sendFamilySymbolParameters(Fmly);

                        /* string FamilyName = Fmly.Name;
                         * foreach (Parameter Param in Fmly.Parameters)
                         * {
                         *   string ParamName = Param.Definition.Name;
                         * }*/
                    }

                    /*
                     * IEnumerable<Element> familiesCollector =
                     * new FilteredElementCollector(commandData.Application.ActiveUIDocument.Document).OfClass(typeof (FamilyInstance))
                     * .WhereElementIsNotElementType()
                     * .Cast<FamilyInstance>()
                     * .GroupBy(fi => fi.Symbol.Family) // (family,familyInstances)
                     * .Select(f=>f.Key);
                     *
                     * foreach (var f in familiesCollector)
                     * {
                     *  COVER.Instance.sendParameters(f);
                     * }*/

                    if (TransactionStatus.Committed != transaction.Commit())
                    {
                        TaskDialog.Show("Failure", "Transaction could not be committed");
                    }
                    return(Autodesk.Revit.UI.Result.Succeeded);
                }
            }
            return(Autodesk.Revit.UI.Result.Failed);
        }
        protected override void TrySolveInstance(IGH_DataAccess DA)
        {
            var elementType = default(DB.ElementType);

            if (!DA.GetData("Type", ref elementType))
            {
                return;
            }

            var filter = default(DB.ElementFilter);

            DA.GetData("Filter", ref filter);

            using (var collector = new DB.FilteredElementCollector(elementType.Document))
            {
                var elementCollector = collector.WhereElementIsNotElementType();

                if (elementType.Category?.Id is DB.ElementId categoryId)
                {
                    elementCollector = elementCollector.OfCategoryId(categoryId);
                }

                if (filter is object)
                {
                    elementCollector = elementCollector.WherePasses(filter);
                }

                DA.SetDataList
                (
                    "Elements",
                    elementCollector.
                    OfTypeId(elementType.Id).
                    Select(x => Types.Element.FromElement(x))
                );
            }
        }
Example #3
0
        /// <summary>
        /// Implement this method as an external command for Revit.
        /// </summary>
        /// <param name="commandData">An object that is passed to the external application
        /// which contains data related to the command,
        /// such as the application object and active view.</param>
        /// <param name="message">A message that can be set by the external application
        /// which will be displayed if a failure or cancellation is returned by
        /// the external command.</param>
        /// <param name="elements">A set of elements to which the external application
        /// can add elements that are to be highlighted in case of failure or cancellation.</param>
        /// <returns>Return the status of the external command.
        /// A result of Succeeded means that the API external method functioned as expected.
        /// Cancelled can be used to signify that the user cancelled the external operation 
        /// at some point. Failure should be returned if the application is unable to proceed with
        /// the operation.</returns>
        public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData,
            ref string message, ElementSet elements)
        {
            using (Transaction transaction = new Transaction(commandData.Application.ActiveUIDocument.Document))
               {
               if (transaction.Start("changeParameters") == TransactionStatus.Started)
               {
                  // ElementId activeOptId = Autodesk.Revit.DB.DesignOption.GetActiveDesignOptionId(commandData.Application.ActiveUIDocument.Document);

                   //ElementDesignOptionFilter filter = new ElementDesignOptionFilter(activeOptId);

                   Autodesk.Revit.DB.FilteredElementCollector collector = new Autodesk.Revit.DB.FilteredElementCollector(commandData.Application.ActiveUIDocument.Document);
                   COVER.Instance.SendGeometry(collector./*WherePasses(filter).*/WhereElementIsNotElementType().GetElementIterator(), commandData.Application.ActiveUIDocument.Document);

                   ElementClassFilter FamilyFilter = new ElementClassFilter(typeof(FamilySymbol));
                   FilteredElementCollector FamilyCollector = new FilteredElementCollector(commandData.Application.ActiveUIDocument.Document);
                   ICollection<Element> AllFamilies = FamilyCollector.WherePasses(FamilyFilter).ToElements();
                   foreach (FamilySymbol Fmly in AllFamilies)
                   {
                       COVER.Instance.sendFamilySymbolParameters(Fmly);
                      /* string FamilyName = Fmly.Name;
                       foreach (Parameter Param in Fmly.Parameters)
                       {
                           string ParamName = Param.Definition.Name;
                       }*/
                   }
            /*
            IEnumerable<Element> familiesCollector =
            new FilteredElementCollector(commandData.Application.ActiveUIDocument.Document).OfClass(typeof (FamilyInstance))
            .WhereElementIsNotElementType()
            .Cast<FamilyInstance>()
            .GroupBy(fi => fi.Symbol.Family) // (family,familyInstances)
            .Select(f=>f.Key);

            foreach (var f in familiesCollector)
            {
            COVER.Instance.sendParameters(f);
            }*/

                   if (TransactionStatus.Committed != transaction.Commit())
                   {
                       TaskDialog.Show("Failure", "Transaction could not be committed");
                   }
                   return Autodesk.Revit.UI.Result.Succeeded;
               }
               }
               return Autodesk.Revit.UI.Result.Failed;
        }
Example #4
0
        void handleMessage(MessageBuffer buf, int msgType,Document doc,UIDocument uidoc, UIApplication app)
        {
            // create Avatar object if not present
             /* if (avatarObject == null)
              {
              createAvatar(doc,uidoc);
              }*/
              switch ((MessageTypes)msgType)
              {

              case MessageTypes.Resend:
                  {
                      Autodesk.Revit.DB.FilteredElementCollector collector = new Autodesk.Revit.DB.FilteredElementCollector(uidoc.Document);
                      COVER.Instance.SendGeometry(collector.WhereElementIsNotElementType().GetElementIterator(), app);

                      ElementClassFilter FamilyFilter = new ElementClassFilter(typeof(FamilySymbol));
                      FilteredElementCollector FamilyCollector = new FilteredElementCollector(uidoc.Document);
                      ICollection<Element> AllFamilies = FamilyCollector.WherePasses(FamilyFilter).ToElements();
                      foreach (FamilySymbol Fmly in AllFamilies)
                      {
                          COVER.Instance.sendFamilySymbolParameters(Fmly);
                      }
                  }
                  break;
              case MessageTypes.SetParameter:
                  {
                      int elemID = buf.readInt();
                      int paramID = buf.readInt();

                      Autodesk.Revit.DB.ElementId id = new Autodesk.Revit.DB.ElementId(elemID);
                      Autodesk.Revit.DB.Element elem = document.GetElement(id);

                      foreach (Autodesk.Revit.DB.Parameter para in elem.Parameters)
                      {
                          if (para.Id.IntegerValue == paramID)
                          {

                              switch (para.StorageType)
                              {
                                  case Autodesk.Revit.DB.StorageType.Double:
                                      double d = buf.readDouble();
                                      try
                                      {
                                          para.Set(d);
                                      }
                                      catch
                                      {
                                          Autodesk.Revit.UI.TaskDialog.Show("Double", "para.Set failed");
                                      }
                                      d = para.AsDouble();
                                      break;
                                  case Autodesk.Revit.DB.StorageType.ElementId:
                                      //find out the name of the element
                                      int tmpid = buf.readInt();
                                      Autodesk.Revit.DB.ElementId eleId = new Autodesk.Revit.DB.ElementId(tmpid);
                                      try
                                      {
                                          para.Set(eleId);
                                      }
                                      catch
                                      {
                                          Autodesk.Revit.UI.TaskDialog.Show("Double", "para.Set failed");
                                      }
                                      break;
                                  case Autodesk.Revit.DB.StorageType.Integer:
                                      try
                                      {
                                          para.Set(buf.readInt());
                                      }
                                      catch
                                      {
                                          Autodesk.Revit.UI.TaskDialog.Show("Double", "para.Set failed");
                                      }
                                      break;
                                  case Autodesk.Revit.DB.StorageType.String:
                                      try
                                      {
                                          para.Set(buf.readString());
                                      }
                                      catch
                                      {
                                          Autodesk.Revit.UI.TaskDialog.Show("Double", "para.Set failed");
                                      }
                                      break;
                                  default:
                                      try
                                      {
                                          para.SetValueString(buf.readString());
                                      }
                                      catch
                                      {
                                          Autodesk.Revit.UI.TaskDialog.Show("Double", "para.Set failed");
                                      }
                                      break;
                              }

                          }
                      }
                  }
                  break;
              case MessageTypes.SetTransform:
                  {
                      int elemID = buf.readInt();
                      double x = buf.readDouble();
                      double y = buf.readDouble();
                      double z = buf.readDouble();

                      Autodesk.Revit.DB.ElementId id = new Autodesk.Revit.DB.ElementId(elemID);
                      Autodesk.Revit.DB.Element elem = document.GetElement(id);
                      Autodesk.Revit.DB.XYZ translationVec = new Autodesk.Revit.DB.XYZ(x, y, z);
                      Autodesk.Revit.DB.LocationCurve ElementPosCurve = elem.Location as Autodesk.Revit.DB.LocationCurve;
                      if (ElementPosCurve != null)
                          ElementPosCurve.Move(translationVec);
                      Autodesk.Revit.DB.LocationPoint ElementPosPoint = elem.Location as Autodesk.Revit.DB.LocationPoint;
                      if (ElementPosPoint != null)
                          ElementPosPoint.Move(translationVec);
                  }
                  break;
              case MessageTypes.UpdateView:
                  {
                      int elemID = buf.readInt();
                      double ex = buf.readDouble();
                      double ey = buf.readDouble();
                      double ez = buf.readDouble();
                      double dx = buf.readDouble();
                      double dy = buf.readDouble();
                      double dz = buf.readDouble();
                      double ux = buf.readDouble();
                      double uy = buf.readDouble();
                      double uz = buf.readDouble();

                      Autodesk.Revit.DB.ElementId id = new Autodesk.Revit.DB.ElementId(elemID);
                      Autodesk.Revit.DB.Element elem = document.GetElement(id);
                      Autodesk.Revit.DB.View3D v3d = (Autodesk.Revit.DB.View3D)elem;
                      Autodesk.Revit.DB.ViewOrientation3D ori = new Autodesk.Revit.DB.ViewOrientation3D(new Autodesk.Revit.DB.XYZ(ex, ey, ez), new Autodesk.Revit.DB.XYZ(ux, uy, uz), new Autodesk.Revit.DB.XYZ(dx, dy, dz));
                      v3d.SetOrientation(ori);
                  }
                  break;

              case MessageTypes.NewAnnotation:
                  {

                      int labelNumber = buf.readInt();
                      double x = buf.readDouble();
                      double y = buf.readDouble();
                      double z = buf.readDouble();
                      double h = buf.readDouble();
                      double p = buf.readDouble();
                      double r = buf.readDouble();
                      string labelText = buf.readString();

                      Autodesk.Revit.DB.XYZ translationVec = new Autodesk.Revit.DB.XYZ(x, y, z);
                      Autodesk.Revit.DB.View view = document.ActiveView;
                      ElementId currentTextTypeId = document.GetDefaultElementTypeId(ElementTypeGroup.TextNoteType);
                      Autodesk.Revit.DB.TextNote tn = Autodesk.Revit.DB.TextNote.Create(document, view.Id, translationVec, labelText, currentTextTypeId);
                      // send back revit ID corresponding to this annotationID
                      // the mapping of annotationIDs to Revit element IDs is done in OpenCOVER
                      MessageBuffer mb = new MessageBuffer();
                      mb.add(labelNumber);
                      mb.add(tn.Id.IntegerValue);
                      sendMessage(mb.buf, MessageTypes.NewAnnotationID);

                  }
                  break;
              case MessageTypes.ChangeAnnotation:
                  {

                      int elemID = buf.readInt();
                      double x = buf.readDouble();
                      double y = buf.readDouble();
                      double z = buf.readDouble();
                      double h = buf.readDouble();
                      double p = buf.readDouble();
                      double r = buf.readDouble();

                      Autodesk.Revit.DB.ElementId id = new Autodesk.Revit.DB.ElementId(elemID);
                      Autodesk.Revit.DB.Element elem = document.GetElement(id);

                      Autodesk.Revit.DB.TextNote tn = elem as Autodesk.Revit.DB.TextNote;
                      if (tn != null)
                      {
                          Autodesk.Revit.DB.XYZ translationVec = new Autodesk.Revit.DB.XYZ(x, y, z);
                          tn.Coord = translationVec;
                      }

                  }
                  break;
              case MessageTypes.SetView:
                  {
                      int currentView = buf.readInt();

                      List<View3D> views = new List<View3D>(
              new FilteredElementCollector(doc)
            .OfClass(typeof(View3D))
            .Cast<View3D>()
            .Where<View3D>(v =>
              v.CanBePrinted && !v.IsTemplate));
                      int n = 0;
                      foreach (View3D v in views)
                      {
                          if (n == currentView)
                          {
                              try
                              {
                              uidoc.ActiveView = v;
                              }
                              catch (Autodesk.Revit.Exceptions.ArgumentNullException e)
                              {
                                  Console.WriteLine("Exception information: {0}", e);
                              }
                              catch (Autodesk.Revit.Exceptions.ArgumentException e)
                              {
                                  Console.WriteLine("Exception information: {0}", e);
                              }
                              catch (Autodesk.Revit.Exceptions.InvalidOperationException e)
                              {
                                  Console.WriteLine("Exception information: {0}", e);
                              }
                              break;
                          }
                          n++;
                      }
                  }
                  break;
              case MessageTypes.ChangeAnnotationText:
                  {

                      int elemID = buf.readInt();
                      string labelText = buf.readString();

                      Autodesk.Revit.DB.ElementId id = new Autodesk.Revit.DB.ElementId(elemID);
                      Autodesk.Revit.DB.Element elem = document.GetElement(id);

                      Autodesk.Revit.DB.TextNote tn = elem as Autodesk.Revit.DB.TextNote;
                      if (tn != null)
                      {
                          tn.Text = labelText;
                      }

                  }
                  break;

              case MessageTypes.AvatarPosition:
                  {
                      double ex = buf.readDouble();
                      double ey = buf.readDouble();
                      double ez = buf.readDouble();
                      double dx = buf.readDouble();
                      double dy = buf.readDouble();
                      double dz = buf.readDouble();
                      Level currentLevel = getLevel(doc, ez);
                      string lev = "";
                      if (currentLevel != null)
                      {
                          lev = currentLevel.Name;
                      }
                      Room testRaum = null;
                      Room currentRoom = null;
                      try
                      {
                          XYZ point = new XYZ(ex, ey, ez);
                          currentRoom = doc.GetRoomAtPoint(point);
                          if (currentRoom == null && (currentLevel != null))
                          {
                              point = new XYZ(ex, ey, currentLevel.ProjectElevation);
                              currentRoom = doc.GetRoomAtPoint(point);
                              if (currentRoom == null)
                              {
                                  testRaum = getRoom(doc, ex, ey, ez);
                                  currentRoom = testRaum;
                              }

                          }
                      }
                      catch
                      {
                      }
                      if (currentRoom != null)
                      {

                          string nr = currentRoom.Number;
                          string name = currentRoom.Name;
                          double area = currentRoom.Area;
                          MessageBuffer mb = new MessageBuffer();
                          mb.add(nr);
                          mb.add(name);
                          mb.add(area);
                          mb.add(lev);
                          sendMessage(mb.buf, MessageTypes.RoomInfo);
                      }
                      else
                      {
                          string nr = "-1";
                          string name = "No Room";
                          double area = 0.0;
                          MessageBuffer mb = new MessageBuffer();
                          mb.add(nr);
                          mb.add(name);
                          mb.add(area);
                          mb.add(lev);
                          sendMessage(mb.buf, MessageTypes.RoomInfo);
                      }
                      if (avatarObject != null)
                      {
                          /*
                          Autodesk.Revit.DB.LocationCurve ElementPosCurve = avatarObject.Location as Autodesk.Revit.DB.LocationCurve;
                          Autodesk.Revit.DB.XYZ translationVec = new Autodesk.Revit.DB.XYZ(ex, ey, ez);
                          ElementPosCurve.Move(translationVec);*/
                      }
                  }
                  break;

              }
        }
Example #5
0
        public static IDictionary MergeElementTypes(DynaElem FromType, DynaElem ToType)
        {
            //  Name of Transaction
            string transactionName = "Merge Element Type";

            // Get the Revit elements from the Dynamo Elements
            RevitDB.ElementType rFromType = (RevitDB.ElementType)FromType.InternalElement;
            RevitDB.ElementType rToType   = (RevitDB.ElementType)ToType.InternalElement;

            RevitDoc document = rToType.Document;

            // Collect all instances of FromType
            RevitDB.FilteredElementCollector collector        = new RevitDB.FilteredElementCollector(document);
            RevitDB.BuiltInParameter         parameterId      = RevitDB.BuiltInParameter.ELEM_TYPE_PARAM;
            RevitDB.FilterNumericEquals      filterNumberRule = new RevitDB.FilterNumericEquals();
            RevitDB.ParameterValueProvider   provider         = new RevitDB.ParameterValueProvider(new RevitDB.ElementId(parameterId));
            RevitDB.FilterRule    filterRule      = new RevitDB.FilterElementIdRule(provider, filterNumberRule, rFromType.Id);
            RevitDB.ElementFilter filterParameter = new RevitDB.ElementParameterFilter(filterRule, false);

            Type instanceType = Select.InstanceClassFromTypeClass(rFromType.GetType());

            if (instanceType != null)
            {
                collector.OfClass(instanceType);
            }

            IEnumerable <RevitDB.Element> instances = collector
                                                      .WhereElementIsNotElementType()
                                                      .WherePasses(filterParameter)
                                                      .ToElements();

            // Intialize list for elements that are successfully merged and failed to merge.
            List <DynaElem> elements       = new List <DynaElem>();
            List <DynaElem> elementsFailed = new List <DynaElem>();

            // Define Function to change instances types.
            Action <IEnumerable <RevitDB.Element> > _SetType = (isntances) =>
            {
                foreach (RevitDB.Element elem in instances)
                {
                    // If Element is in a group, put the element in the failed list
                    int groupId = elem.GroupId.IntegerValue;
                    if (groupId == -1)
                    {
                        //elem.TextNoteType = rToType;
                        RevitDB.Parameter param = elem.get_Parameter(RevitDB.BuiltInParameter.ELEM_TYPE_PARAM);
                        param.Set(rToType.Id);
                        DynaElem dElem = elem.ToDSType(true);
                        elements.Add(dElem);
                    }
                    else
                    {
                        DynaElem dElem = elem.ToDSType(true);
                        elementsFailed.Add(dElem);
                    }
                }

                // Check if there are any instances of FromType left
                int count = collector.Count();
                if (count == 0)
                {
                    document.Delete(rFromType.Id);
                }
            };

            if (document.IsModifiable)
            {
                TransactionManager.Instance.EnsureInTransaction(document);
                _SetType(instances);
                TransactionManager.Instance.TransactionTaskDone();
            }
            else
            {
                using (Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(document))
                {
                    trans.Start(transactionName);
                    _SetType(instances);
                    trans.Commit();
                }
            }

            return(new Dictionary <string, object>
            {
                { "Merged", elements },
                { "Failed", elementsFailed }
            });
        }