//
// (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.Collections.ObjectModel;
using System.Text;
using System.Linq;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB.Structure;
namespace Revit.SDK.Samples.Reinforcement.CS
{
///
/// The interface for the family instance reinforcement creation.
/// The main method is Run(), which used to create the reinforcement
///
public interface IFrameReinMaker
{
///
/// Main function of Maker interface
///
/// indicate the result of run
bool Run();
}
///
/// The base class for family instance reinforcement creation.
/// It only implement the Run() method. which give the flow process for creation.
///
public class FramReinMaker : IFrameReinMaker
{
///
/// the API create handle
///
protected Autodesk.Revit.DB.Document m_revitDoc;
///
/// the family instance to places rebar on
///
protected FamilyInstance m_hostObject;
///
/// a set to store all the rebar types
///
protected List m_rebarTypes = new List();
///
/// a list to store all the hook types
///
protected List m_hookTypes = new List();
///
/// Show all the rebar types in revit
///
public IList RebarTypes
{
get
{
return m_rebarTypes;
}
}
///
/// Show all the rebar hook types in revit
///
public IList HookTypes
{
get
{
return m_hookTypes;
}
}
///
/// Implement the Run() method of IFrameReinMaker interface.
/// Give the flew process of the reinforcement creation.
///
///
bool IFrameReinMaker.Run()
{
// First, check the data whether is right and enough.
if (!AssertData())
{
return false;
}
// Second, show a form to the user to collect creation information
if (!DisplayForm())
{
return false;
}
// At last, begin to create the reinforcement rebars
if (!FillWithBars())
{
return false;
}
return true;
}
///
/// This is a virtual method which used to check the data whether is right and enough.
///
/// true if the the data is right and enough, otherwise false.
protected virtual bool AssertData()
{
return true; // only return true
}
///
/// This is a virtual method which used to collect creation information
///
/// true if the informatin collection is successful, otherwise false
protected virtual bool DisplayForm()
{
return true; // only return true
}
///
/// This is a virtual method which used to create reinforcement.
///
/// true if the creation is successful, otherwise false
protected virtual bool FillWithBars()
{
return true; // only return true
}
///
/// The constructor of FramReinMaker
///
/// the ExternalCommandData reference
/// the host family instance
protected FramReinMaker(ExternalCommandData commandData, FamilyInstance hostObject)
{
// Get and store reinforcement create handle and host family instance
m_revitDoc = commandData.Application.ActiveUIDocument.Document;
m_hostObject = hostObject;
// Get all the rebar types in revit
if (!GetRebarTypes(commandData))
{
throw new Exception("Can't get any rebar type from revit.");
}
// Get all the rebar hook types in revit
if (!GetHookTypes(commandData))
{
throw new Exception("Can't get any rebar hook type from revit.");
}
}
///
/// The helper function to changed rebar number and spacing properties
///
/// The rebar instance which need to modify
/// The rebar number want to set
/// The spacing want to set
protected static void SetRebarSpaceAndNumber(Rebar bar, int number, double spacing)
{
// Asset the parameter is valid
if (null == bar || 2 > number || 0 > spacing)
{
return;
}
// Change the rebar number and spacing properties
bar.GetShapeDrivenAccessor().SetLayoutAsNumberWithSpacing(number, spacing, true, true, true);
}
///
/// A wrap fuction which used to create the reinforcement.
///
/// The element of RebarBarType
/// The element of start RebarHookType
/// The element of end RebarHookType
/// The goemetry information of the rebar
/// An Integer defines the orientation of the start hook
/// An Integer defines the orientation of the end hook
///
protected Rebar PlaceRebars(RebarBarType rebarType, RebarHookType startHook,
RebarHookType endHook, RebarGeometry geomInfo,
RebarHookOrientation startOrient, RebarHookOrientation endOrient)
{
Autodesk.Revit.DB.XYZ normal = geomInfo.Normal; // the direction of rebar distribution
IList curves = geomInfo.Curves; // the shape of the rebar curves
// Invoke the NewRebar() method to create rebar
Rebar createdRebar = Rebar.CreateFromCurves(m_revitDoc, Autodesk.Revit.DB.Structure.RebarStyle.Standard, rebarType, startHook, endHook,
m_hostObject, normal, curves,
startOrient, endOrient, false, true);
if (null == createdRebar) // Assert the creation is successful
{
return null;
}
// Change the rebar number and spacing properties to the user wanted
SetRebarSpaceAndNumber(createdRebar, geomInfo.RebarNumber, geomInfo.RebarSpacing);
return createdRebar;
}
///
/// get all the hook types in current project, and store in m_hookTypes data
///
/// the ExternalCommandData reference
/// true if some hook types can be gotton, otherwise false
private bool GetHookTypes(ExternalCommandData commandData)
{
// Initialize the m_hookTypes which used to store all hook types.
FilteredElementCollector filteredElementCollector = new FilteredElementCollector(commandData.Application.ActiveUIDocument.Document);
filteredElementCollector.OfClass(typeof(RebarHookType));
m_hookTypes = filteredElementCollector.Cast().ToList();
// If no hook types in revit return false, otherwise true
return (0 == m_hookTypes.Count) ? false : true;
}
///
/// get all the rebar types in current project, and store in m_rebarTypes data
///
/// the ExternalCommandData reference
/// true if some rebar types can be gotton, otherwise false
private bool GetRebarTypes(ExternalCommandData commandData)
{
// Initialize the m_rebarTypes which used to store all rebar types.
// Get all rebar types in revit and add them in m_rebarTypes
FilteredElementCollector filteredElementCollector = new FilteredElementCollector(commandData.Application.ActiveUIDocument.Document);
filteredElementCollector.OfClass(typeof(RebarBarType));
m_rebarTypes = filteredElementCollector.Cast().ToList();
// If no rebar types in revit return false, otherwise true
return (0 == m_rebarTypes.Count) ? false : true;
}
}
}