应用程序名称: GetSetDefaultTypes

Revit 平台: 所有版本

Revit 版本: 2015.0

首次发布版本: 2015.0

编程语言: C#

技能要求: 中等水平

类别: 元素,

类型: ExternalCommand ExternalApplication

主题: 获取和设置默认族或元素类型

概要: GetSetDefaultTypes 是一个示例插件,演示了如何获取和设置默认的族和元素类型。

相关类:

        Autodesk.Revit.DB.Document.GetDefaultFamilyTypeId

Autodesk.Revit.DB.Document.SetDefaultFamilyTypeId

        Autodesk.Revit.DB.Document.IsDefaultFamilyTypeIdValid

        Autodesk.Revit.DB.ElementType.IsValidDefaultFamilyType

        Autodesk.Revit.DB.Document.GetDefaultElementTypeId

        Autodesk.Revit.DB.Document.SetDefaultElementTypeId

        Autodesk.Revit.DB.Document.IsDefaultElementTypeIdValid

项目文件:

Command.cs

它包含了类 Command,该类继承自接口 IExternalCommand,并实现了 Execute 方法。

说明: 构建并安装插件,然后启动 Revit。在打开文档后,选择“默认类型选择器”选项卡中的 “Type” 显示在 “Ribbon” 上的按钮来启动族和元素类型选择器。选择相应的族或元素类型来更改默认的族或元素类型。您可以从类型属性对话框中看到更改。

源代码:

完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码

ThisApplication.cs

//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using System.Timers;
using Autodesk.Revit.UI.Events;
using System.Windows.Media.Imaging;
using System.Windows;

using Autodesk.Revit;
using System.Net;
using System.IO;
using System.Reflection;

namespace Revit.SDK.Samples.GetSetDefaultTypes.CS
{
   /// <summary>
   /// Implements the Revit add-in interface IExternalApplication
   /// </summary>
   public class ThisApplication : IExternalApplication
   {
      #region IExternalApplication Members

      public Result OnShutdown(UIControlledApplication application)
      {
         return Result.Succeeded;
      }

      public Result OnStartup(UIControlledApplication application)
      {
         try
         {
            string str = "Default Type Selector";
            RibbonPanel panel = application.CreateRibbonPanel(str);
            string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            PushButtonData data = new PushButtonData("Default Type Selector", "Default Type Selector", directoryName + @"\GetSetDefaultTypes.dll", "Revit.SDK.Samples.GetSetDefaultTypes.CS.ThisCommand");
            PushButton button = panel.AddItem(data) as PushButton;
            button.LargeImage = new BitmapImage(new Uri(directoryName + "\\Resources\\type.png"));

            // register dockable Windows on startup.
            DefaultFamilyTypesPane = new DefaultFamilyTypes();
            DefaultElementTypesPane = new DefaultElementTypes();
            application.RegisterDockablePane(DefaultFamilyTypes.PaneId, "Default Family Types", DefaultFamilyTypesPane);
            application.RegisterDockablePane(DefaultElementTypes.PaneId, "Default Element Types", DefaultElementTypesPane);

            // register view active event
            application.ViewActivated += new EventHandler<ViewActivatedEventArgs>(application_ViewActivated);

            return Result.Succeeded;
         }
         catch (Exception exception)
         {
            MessageBox.Show(exception.ToString(), "Default Type Selector");
            return Result.Failed;
         }
      }

      #endregion

      public static DefaultFamilyTypes DefaultFamilyTypesPane;
      public static DefaultElementTypes DefaultElementTypesPane;


      /// <summary>
      /// Show dockable panes when view active.
      /// </summary>
      void application_ViewActivated(object sender, Autodesk.Revit.UI.Events.ViewActivatedEventArgs e)
      {
         if (!DockablePane.PaneExists(DefaultFamilyTypes.PaneId) ||
             !DockablePane.PaneExists(DefaultElementTypes.PaneId))
            return;

         UIApplication uiApp = sender as UIApplication;
         if (uiApp == null)
            return;

         if (DefaultFamilyTypesPane != null)
            DefaultFamilyTypesPane.SetDocument(e.Document);
         if (DefaultElementTypesPane != null)
            DefaultElementTypesPane.SetDocument(e.Document);
      }

   }
}

ThisCommand.cs

//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.UI;

namespace Revit.SDK.Samples.GetSetDefaultTypes.CS
{
   [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
   class ThisCommand : IExternalCommand
   {
      #region IExternalCommand Members

      public Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
      {
         if (!DockablePane.PaneExists(DefaultFamilyTypes.PaneId) ||
             !DockablePane.PaneExists(DefaultElementTypes.PaneId))
            return Result.Failed;

         UIApplication uiApp = commandData.Application;
         if (uiApp == null)
            return Result.Failed;

         DockablePane pane = uiApp.GetDockablePane(DefaultFamilyTypes.PaneId);
         pane.Show();
         DockablePane elemTypePane = uiApp.GetDockablePane(DefaultElementTypes.PaneId);
         elemTypePane.Show();

         if (ThisApplication.DefaultFamilyTypesPane != null)
            ThisApplication.DefaultFamilyTypesPane.SetDocument(commandData.Application.ActiveUIDocument.Document);
         if (ThisApplication.DefaultElementTypesPane != null)
            ThisApplication.DefaultElementTypesPane.SetDocument(commandData.Application.ActiveUIDocument.Document);

         return Result.Succeeded;
      }

      #endregion
   }
}