天天看點

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();
}
粘貼的老外的