Navisworks Api Quantification

Quantification  国外有的叫定量  我们国内一些施工方叫工程量。

通过TakeOff API的开发者有机会获得更多的数据和数据可通过图形用户界面。

1 添加Navisworks的Api

Autodesk.Navisworks.Takeoff.dll

2获取量一般是由随模型一起有个Access数据库.所有数据存在该数据库里面。当然也可以是基本数据库..
3使用C#和使用Autodesk.Navisworks.Api.Takeoff命名空间则扩展方法:

DocumentExtensions.GetTakeoff 

例如 C# 代码:

DocumentTakeoff docTakeoff =
   Autodesk.Navisworks.Api.Application.MainDocument.Takeoff as DocumentTakeoff;

DocumentTakeoff docTakeoff2 =
   Autodesk.Navisworks.Api.Application.MainDocument.GetTakeoff();
注:下面有关键字 table 都是数据库里的表。
DocumentExtensions类的地方扩展到文档类的DocumentTakeoff文档部件类。
 
DocumentTakeoff提供对与起飞相关的各种文档部分。
 
ItemGroupTable配置信息ItemGorupTable。
 
ItemTable配置信息ItemTable。
 
ObjectResourceTable配置信息ObjectResourceTable。
 
ObjectStepTable配置信息ObjectStepTable。
 
ObjectTable配置信息ObjectTable。
 
ResourceGroupTable配置信息ResourceGroupTable。
 
ResourceTable配置信息ResourceTable。
 
StepResourceTable配置信息StepResourceTable。
 
StepTable配置信息StepTable。
 
TakeoffTable为所有表共有的特征共同的基类。
 
TakeoffTableSelection表示在一个表中选择。
 
TakeoffColumnDefinition起飞表中的固定列的定义。
 
TakeoffProjectSettings Access项目设置。
 
TakeoffSelection切入点每个表的选择。
 
TakeoffSelectionChangeEventArgs用于TakeoffSelection Changed事件通过哪些表被修改。
 
TakeoffSheetIds访问表编号。
 
TakeoffVariable代表一个变量。
 
TakeoffVariableCollection代表配置列的行数据。
 
TakeoffVariableDefinition起飞表变量列的定义。
 
的配置列定义TakeoffVariableDefinitionCollection集合

4使用标准的SQL查询数据

Int64 GetLastInsertRowId()
{
   DocumentTakeoff docTakeoff = Autodesk.Navisworks.Api.Application.MainDocument.GetTakeoff();
   using (NavisworksCommand cmd = docTakeoff.Database.Value.CreateCommand())
   {
      //use SELECT ... FROM ... WHERE ... sql for query.
      //last_insert_rowid() is a stored function used to retrieve the rowid of the last insert row
      cmd.CommandText = "select last_insert_rowid()";
      using (NavisWorksDataReader dataReader = cmd.ExecuteReader())
      {
         Int64 lastId = -1;
         if (dataReader.Read())
         {
            Int64.TryParse(dataReader[0].ToString(), out lastId);
         }
         return lastId;
      }
   }
}

5使用标准的SQL创建目录

Int64 InsertItem(Int64? parent, String name, String description, String wbs, Int32 color, Double transparency)
{
   Debug.Assert(name != null);
   DocumentTakeoff docTakeoff = Autodesk.Navisworks.Api.Application.MainDocument.GetTakeoff();
   ItemTable table = docTakeoff.Items;
   Debug.Assert(table != null);

   //Directly operate on database
   //Database schema entry: TakeoffTable
   //INSERT INTO TABLE(COL1,COL2,COL3...) VALUES(V1,V2,V3...);
   String sql = "INSERT INTO TK_ITEM(parent, name, description, wbs, color, transparency) VALUES(@parent, @name, @description,@wbs, @color,@transparency)";
   //Modification must be surrounded by NavisworksTransaction
   using (NavisworksTransaction trans = docTakeoff.Database.BeginTransaction(DatabaseChangedAction.Edited))
   {
      using (NavisworksCommand cmd = docTakeoff.Database.Value.CreateCommand())
      {
         NavisworksParameter p = cmd.CreateParameter();
         p.ParameterName = "@parent";
         if (parent.HasValue)
            p.Value = parent.Value;
         else
            p.Value = null;
         cmd.Parameters.Add(p);

         p = cmd.CreateParameter();
         p.ParameterName = "@name";
         p.Value = name;
         cmd.Parameters.Add(p);

         p = cmd.CreateParameter();
         p.ParameterName = "@description";
         p.Value = description;
         cmd.Parameters.Add(p);

         p = cmd.CreateParameter();
         p.ParameterName = "@wbs";
         p.Value = wbs;
         cmd.Parameters.Add(p);

         p = cmd.CreateParameter();
         p.ParameterName = "@color";
         p.Value = color;
         cmd.Parameters.Add(p);

         p = cmd.CreateParameter();
         p.ParameterName = "@transparency";
         p.Value = transparency;
         cmd.Parameters.Add(p);

         cmd.CommandText = sql;
         cmd.ExecuteNonQuery();
      }
      trans.Commit();
   }
   return GetLastInsertRowId();
}

6复制使用类来创建一个takeOff

Int64 DoTakeoff(Int64 itemId, Guid modelItemGuid)
{
   DocumentTakeoff docTakeoff = Autodesk.Navisworks.Api.Application.MainDocument.GetTakeoff();
   List<Autodesk.Navisworks.Api.ModelItem> items = Autodesk.Navisworks.Api.Application.MainDocument.Models.RootItemDescendantsAndSelf.WhereInstanceGuid(modelItemGuid).ToList();
   Int64 lastId = -1;
   if (items.Count != 0)
   {
      using (NavisworksTransaction trans = docTakeoff.Database.BeginTransaction(DatabaseChangedAction.Edited))
      {
         docTakeoff.Objects.InsertModelItemTakeoff(itemId, items[0]);
         //Quantification UI actually expect the takeoff to have a non-empty wbs, so better to set the wbs for it using the sql way
         lastId = GetLastInsertRowId();
         Debug.Assert(lastId > 0);
         using (NavisworksCommand cmd = docTakeoff.Database.Value.CreateCommand())
         {
            //UPDATE Object set WBS = value WHERE id = lastId;
            cmd.CommandText = "UPDATE TK_OBJECT SET wbs = @wbs WHERE id = @id";
            NavisworksParameter p = cmd.CreateParameter();
            p.ParameterName = "@wbs";
            p.Value = 1;
            cmd.Parameters.Add(p);

            p = cmd.CreateParameter();
            p.ParameterName = "@id";
            p.Value = lastId;
            cmd.Parameters.Add(p);

            cmd.ExecuteNonQuery();
         }
         trans.Commit();
      }

   }
   return lastId;
}

7使用类查询和修改数据

void UpdateTakeoffValue(Int64 objectId)
{
   DocumentTakeoff docTakeoff = Autodesk.Navisworks.Api.Application.MainDocument.GetTakeoff();
   //TakeoffVariableCollection TakeoffVariable are the entrance for read/update of the variables
   TakeoffVariableCollection variableCollection = docTakeoff.Objects.SelectInputVariables(objectId);
   Int32 lengthIndex = variableCollection.Find("ModelLength");
   if (lengthIndex != -1)
   {
      TakeoffVariable lengthVariable = variableCollection.GetItem(lengthIndex);
      if (lengthVariable.IsAbleToSetValue)
      {
         lengthVariable.Value = Autodesk.Navisworks.Api.VariantData.FromDouble(5.6);
         using (NavisworksTransaction trans = docTakeoff.Database.BeginTransaction(DatabaseChangedAction.Edited))
         {
            docTakeoff.Objects.UpdateInputVariables(objectId, variableCollection);
            trans.Commit();
         }
      }
   }
}

8选择在不同层次的元素在层次结构中

void SelectUIItem(Int64 itemId)
{
   TakeoffSelection takeoffSelection = Autodesk.Navisworks.Api.Application.MainDocument.GetTakeoff().CurrentSelection;
   takeoffSelection.BeginEdit();
   takeoffSelection.Items.Clear();
   takeoffSelection.ItemGroups.Clear();
   takeoffSelection.StepResources.Clear();
   takeoffSelection.Steps.Clear();
   takeoffSelection.Items.Add(itemId);
   takeoffSelection.EndEdit();
}
粘贴的老外的
posted @ 2014-12-09 10:05  在 水 一 方  阅读(1936)  评论(0编辑  收藏  举报