290 lines
15 KiB
C#
290 lines
15 KiB
C#
//
|
|
// (C) Copyright 2003-2017 by Autodesk, Inc. All rights reserved.
|
|
//
|
|
// 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 ITS 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.DB;
|
|
using Autodesk.Revit.UI;
|
|
|
|
namespace Revit.SDK.Samples.ScheduleCreation.CS
|
|
{
|
|
/// <summary>
|
|
/// Utility class that contains methods of view schedule creation and schedule sheet instance creation.
|
|
/// </summary>
|
|
class SleeveScheduleCreationUtility
|
|
{
|
|
private static BuiltInParameter[] s_skipParameters = new BuiltInParameter[] { BuiltInParameter.ALL_MODEL_MARK };
|
|
|
|
/// <summary>
|
|
/// Create view schedule(s) and add them to sheet.
|
|
/// </summary>
|
|
/// <param name="uiDocument">UIdocument of revit file.</param>
|
|
public void CreateAndAddSchedules(UIDocument uiDocument/*, List<FamilyInstance> lst*/)
|
|
{
|
|
TransactionGroup tGroup = new TransactionGroup(uiDocument.Document, "Create schedules and sheets");
|
|
tGroup.Start();
|
|
|
|
|
|
ICollection<ViewSchedule> schedules = CreateSchedules(uiDocument);
|
|
|
|
foreach (ViewSchedule schedule in schedules)
|
|
{
|
|
AddScheduleToNewSheet(uiDocument.Document, schedule);
|
|
}
|
|
|
|
tGroup.Assimilate();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a sheet to show the schedule.
|
|
/// </summary>
|
|
/// <param name="document">DBDocument of revit file.</param>
|
|
/// <param name="schedule">View schedule which will be shown on sheet.</param>
|
|
private void AddScheduleToNewSheet(Document document, ViewSchedule schedule)
|
|
{
|
|
//Create a filter to get all the title block types.
|
|
FilteredElementCollector collector = new FilteredElementCollector(document);
|
|
collector.OfCategory(BuiltInCategory.OST_TitleBlocks);
|
|
collector.WhereElementIsElementType();
|
|
|
|
Transaction t = new Transaction(document, "Create and populate sheet");
|
|
t.Start();
|
|
|
|
//Get ElementId of first title block type.
|
|
ElementId titleBlockId = collector.FirstElementId();
|
|
|
|
//Create sheet by gotten title block type.
|
|
ViewSheet newSheet = ViewSheet.Create(document, titleBlockId);
|
|
newSheet.Name = "Sheet for " + schedule.Name;
|
|
|
|
document.Regenerate();
|
|
|
|
//Declare a XYZ to be used as the upperLeft point of schedule sheet instance to be created.
|
|
XYZ upperLeft = new XYZ();
|
|
|
|
//If there is an existing title block.
|
|
if (titleBlockId != ElementId.InvalidElementId)
|
|
{
|
|
//Find titleblock of the newly created sheet.
|
|
collector = new FilteredElementCollector(document);
|
|
collector.OfCategory(BuiltInCategory.OST_TitleBlocks);
|
|
collector.OwnedByView(newSheet.Id);
|
|
Element titleBlock = collector.FirstElement();
|
|
|
|
//Get bounding box of the title block.
|
|
BoundingBoxXYZ bbox = titleBlock.get_BoundingBox(newSheet);
|
|
|
|
//Get upperLeft point of the bounding box.
|
|
upperLeft = new XYZ(bbox.Min.X, bbox.Max.Y, bbox.Min.Z);
|
|
//Move the point to the postion that is 2 inches right and 2 inches down from the original upperLeft point.
|
|
upperLeft = upperLeft + new XYZ(2.0 / 12.0, -2.0 / 12.0, 0);
|
|
}
|
|
|
|
//Create a new schedule sheet instance that makes the sheet to show the data of wall view schedule at upperLeft point.
|
|
ScheduleSheetInstance placedInstance = ScheduleSheetInstance.Create(document, newSheet.Id, schedule.Id, upperLeft);
|
|
|
|
t.Commit();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a view schedule of wall category and add schedule field, filter and sorting/grouping field to it.
|
|
/// </summary>
|
|
/// <param name="uiDocument">UIdocument of revit file.</param>
|
|
/// <returns>ICollection of created view schedule(s).</returns>
|
|
private ICollection<ViewSchedule> CreateSchedules(UIDocument uiDocument)
|
|
{
|
|
Document document = uiDocument.Document;
|
|
|
|
Transaction t = new Transaction(document, "Create Schedules");
|
|
t.Start();
|
|
|
|
List<ViewSchedule> schedules = new List<ViewSchedule>();
|
|
//ScheduleFilter filter = new ScheduleFilter();
|
|
|
|
//Create an empty view schedule of wall category.
|
|
ViewSchedule schedule = ViewSchedule.CreateSchedule(document, new ElementId(BuiltInCategory.OST_GenericModel), ElementId.InvalidElementId);
|
|
schedule.Name = "Sleeve Schedule";
|
|
//schedule.Definition.AddFilter(filter);
|
|
schedules.Add(schedule);
|
|
|
|
|
|
|
|
|
|
|
|
IList<SchedulableField> lst =schedule.Definition.GetSchedulableFields();
|
|
|
|
//Iterate all the schedulable field gotten from the walls view schedule.
|
|
foreach (SchedulableField schedulableField in schedule.Definition.GetSchedulableFields())
|
|
{
|
|
//Judge if the FieldType is ScheduleFieldType.Instance.
|
|
if (schedulableField.FieldType == ScheduleFieldType.Instance)
|
|
{
|
|
//Get ParameterId of SchedulableField.
|
|
ElementId parameterId = schedulableField.ParameterId;
|
|
|
|
//If the ParameterId is id of BuiltInParameter.ALL_MODEL_MARK then ignore next operation.
|
|
if (ShouldSkip(parameterId))
|
|
continue;
|
|
|
|
|
|
if (schedulableField.GetName(document).CompareTo("Slv_Name") == 0 ||
|
|
schedulableField.GetName(document).CompareTo("Slv_Dt_Width") == 0 ||
|
|
schedulableField.GetName(document).CompareTo("Slv_Dt_Height") == 0 ||
|
|
schedulableField.GetName(document).CompareTo("Slv_RD_Dia") == 0 ||
|
|
schedulableField.GetName(document).CompareTo("Slv_Pi_Dia") == 0 ||
|
|
schedulableField.GetName(document).CompareTo("Slv_Slv_Dia") == 0 ||
|
|
schedulableField.GetName(document).CompareTo("Slv_Slv_Width") == 0 ||
|
|
schedulableField.GetName(document).CompareTo("Slv_Slv_Height") == 0 ||
|
|
schedulableField.GetName(document).CompareTo("Slv_Ins_Thk") == 0 ||
|
|
schedulableField.GetName(document).CompareTo("Slv_CT_Height") == 0 ||
|
|
schedulableField.GetName(document).CompareTo("Slv_CT_Width") == 0 ||
|
|
schedulableField.GetName(document).CompareTo("Slv_Slv_Length") == 0 ||
|
|
schedulableField.GetName(document).CompareTo("Slv_Slv_Level") == 0 ||
|
|
schedulableField.GetName(document).CompareTo("Slv_Slv_Type") == 0 ||
|
|
schedulableField.GetName(document).CompareTo("Slv_Slv_InstallType") == 0 ||
|
|
|
|
schedulableField.ParameterId.IntegerValue == -1002052)
|
|
{
|
|
//Add a new schedule field to the view schedule by using the SchedulableField as argument of AddField method of Autodesk.Revit.DB.ScheduleDefinition class.
|
|
ScheduleField field = schedule.Definition.AddField(schedulableField);
|
|
|
|
|
|
if (field.GetName().CompareTo("Slv_Dt_Width") == 0)
|
|
field.ColumnHeading = "덕트 넓이";
|
|
else if (field.GetName().CompareTo("Slv_Dt_Height") == 0)
|
|
field.ColumnHeading = "덕트 높이";
|
|
else if (field.GetName().CompareTo("Slv_RD_Dia") == 0)
|
|
field.ColumnHeading = "원형 덕트 지름";
|
|
else if (field.GetName().CompareTo("Slv_Pi_Dia") == 0)
|
|
field.ColumnHeading = "파이프 직경";
|
|
else if (field.GetName().CompareTo("Slv_Slv_Dia") == 0)
|
|
field.ColumnHeading = "원형 슬리브 직경";
|
|
else if (field.GetName().CompareTo("Slv_Slv_Width") == 0)
|
|
field.ColumnHeading = "슬리브 넓이";
|
|
else if (field.GetName().CompareTo("Slv_Slv_Height") == 0)
|
|
field.ColumnHeading = "슬리브 높이";
|
|
else if (field.GetName().CompareTo("Slv_Ins_Thk") == 0)
|
|
field.ColumnHeading = "단열재 두께";
|
|
else if (field.GetName().CompareTo("Slv_CT_Height") == 0)
|
|
field.ColumnHeading = "케이블 트레이 높이";
|
|
else if (field.GetName().CompareTo("Slv_CT_Width") == 0)
|
|
field.ColumnHeading = "케이블 트레이 넓이";
|
|
else if (field.GetName().CompareTo("Slv_Slv_Length") == 0)
|
|
field.ColumnHeading = "슬리브 길이";
|
|
else if (field.GetName().CompareTo("Slv_Slv_Level") == 0)
|
|
field.ColumnHeading = "슬리브 설치 레벨";
|
|
else if (field.GetName().CompareTo("Slv_Slv_Type") == 0)
|
|
field.ColumnHeading = "슬리브 타입";
|
|
else if (field.GetName().CompareTo("Slv_Slv_InstallType") == 0)
|
|
field.ColumnHeading = "슬리브 설치 타입";
|
|
else if (field.GetName().CompareTo("Slv_Name") == 0)
|
|
field.IsHidden = true;
|
|
if (schedulableField.ParameterId == new ElementId(BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM))
|
|
{
|
|
field.ColumnHeading = "종류";
|
|
}
|
|
|
|
//Judge if the parameterId is a BuiltInParameter.
|
|
if (Enum.IsDefined(typeof(BuiltInParameter), parameterId.IntegerValue))
|
|
{
|
|
BuiltInParameter bip = (BuiltInParameter)parameterId.IntegerValue;
|
|
//Get the StorageType of BuiltInParameter.
|
|
StorageType st = document.get_TypeOfStorage(bip);
|
|
//if StorageType is String or ElementId, set GridColumnWidth of schedule field to three times of current GridColumnWidth.
|
|
//And set HorizontalAlignment property to left.
|
|
if (st == StorageType.String || st == StorageType.ElementId)
|
|
{
|
|
field.GridColumnWidth = 3 * field.GridColumnWidth;
|
|
field.HorizontalAlignment = ScheduleHorizontalAlignment.Left;
|
|
}
|
|
//For other StorageTypes, set HorizontalAlignment property to center.
|
|
else
|
|
{
|
|
field.HorizontalAlignment = ScheduleHorizontalAlignment.Center;
|
|
}
|
|
}
|
|
//슬리브 필터링
|
|
if (field.GetName().CompareTo("Slv_Name") == 0)
|
|
{
|
|
ScheduleFilter filter = new ScheduleFilter(field.FieldId, ScheduleFilterType.Equal, "sleeve");
|
|
schedule.Definition.AddFilter(filter);
|
|
}
|
|
//그룹화
|
|
if (field.ParameterId == new ElementId(BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM))
|
|
{
|
|
ScheduleSortGroupField sortGroupField = new ScheduleSortGroupField(field.FieldId);
|
|
//sortGroupField.ShowHeader = true;
|
|
sortGroupField.ShowFooter = true;
|
|
sortGroupField.ShowFooterCount = true;
|
|
sortGroupField.ShowFooterTitle = true;
|
|
schedule.Definition.AddSortGroupField(sortGroupField);
|
|
}
|
|
}
|
|
|
|
/*}
|
|
|
|
|
|
//Filter the view schedule by volume
|
|
if (field.ParameterId == new ElementId(BuiltInParameter.HOST_VOLUME_COMPUTED))
|
|
{
|
|
double volumeFilterInCubicFt = 0.8 * Math.Pow(3.2808399, 3.0);
|
|
ScheduleFilter filter = new ScheduleFilter(field.FieldId, ScheduleFilterType.GreaterThan, volumeFilterInCubicFt);
|
|
schedule.Definition.AddFilter(filter);
|
|
}
|
|
|
|
//Group and sort the view schedule by type
|
|
if (field.ParameterId == new ElementId(BuiltInParameter.ELEM_TYPE_PARAM))
|
|
{
|
|
ScheduleSortGroupField sortGroupField = new ScheduleSortGroupField(field.FieldId);
|
|
sortGroupField.ShowHeader = true;
|
|
schedule.Definition.AddSortGroupField(sortGroupField);
|
|
}*/
|
|
}
|
|
}
|
|
|
|
|
|
t.Commit();
|
|
|
|
uiDocument.ActiveView = schedule;
|
|
|
|
return schedules;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Judge if the parameterId should be skipped.
|
|
/// </summary>
|
|
/// <param name="parameterId">ParameterId to be judged.</param>
|
|
/// <returns>Return true if parameterId should be skipped.</returns>
|
|
private bool ShouldSkip(ElementId parameterId)
|
|
{
|
|
foreach (BuiltInParameter bip in s_skipParameters)
|
|
{
|
|
if (new ElementId(bip) == parameterId)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|