//
// (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.Text;
using System.Windows.Forms;
using Autodesk;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB.Structure;
namespace Revit.SDK.Samples.Loads.CS
{
///
/// Implements the Revit add-in interface IExternalCommand
///
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
public class Loads : IExternalCommand
{
#region Private Data Members
// Mainly used data definition
Autodesk.Revit.ApplicationServices.Application m_revit; // Store the reference of revit
LoadCombinationDeal m_combinationDeal; // the deal class on load combination page
LoadCaseDeal m_loadCaseDeal; // the deal class on load case page
String m_errorInformation; // Store the error information
// Define the data mainly used in LoadCombinationDeal class
List m_usageNameList; // Store all the usage names in current document
List m_loadUsageList; // Used to store all the load usages
List m_combinationNameList; // Store all the combination names in current document
List m_LoadCombinationMap;
// Store all the Load Combination information include the user add.
List m_formulaMap; // Store the formula information the user add
List m_usageMap;
// Define the data mainly used in LoadCaseDeal class
List m_loadCasesCategory; //Store the load case's category
List m_loadCases; //Store all the load cases in current document
List m_loadNatures; //Store all the load natures in current document
List m_loadCasesMap; // Store all the load case information include the user add.
List m_loadNaturesMap; //Store all the load natures information
#endregion
#region Properties
///
/// Used as the dataSource of load cases DataGridView control,
/// and the information which support load case creation also.
///
public List LoadCasesMap
{
get
{
return m_loadCasesMap;
}
}
///
/// Used as the dataSource of load natures DataGridView control,
/// and the information which support load nature creation also.
///
public List LoadNaturesMap
{
get
{
return m_loadNaturesMap;
}
}
///
/// save all loadnature object in current project
///
public List LoadNatures
{
get
{
return m_loadNatures;
}
}
///
/// save all loadcase object in current project
///
public List LoadCases
{
get
{
return m_loadCases;
}
}
///
/// save all load cases category in current project
///
public List LoadCaseCategories
{
get
{
return m_loadCasesCategory;
}
}
///
/// object which do add, delete and update command on load related objects
///
public LoadCaseDeal LoadCasesDeal
{
get
{
return m_loadCaseDeal;
}
}
///
/// Store the reference of revit
///
public Autodesk.Revit.ApplicationServices.Application RevitApplication
{
get
{
return m_revit;
}
}
///
/// LoadUsageNames property, used to store all the usage names in current document
///
public List LoadUsageNames
{
get
{
return m_usageNameList;
}
}
///
/// Used to store all the load usages in current document, include the user add
///
public List LoadUsages
{
get
{
return m_loadUsageList;
}
}
///
/// LoadCombinationNames property, used to store all the combination names in current document
///
public List LoadCombinationNames
{
get
{
return m_combinationNameList;
}
}
///
/// Show the error information while contact with revit
///
public String ErrorInformation
{
get
{
return m_errorInformation;
}
set
{
m_errorInformation = value;
}
}
///
/// Used as the dataSource of load combination DataGridView control,
/// and the information which support load combination creation also.
///
public List LoadCombinationMap
{
get
{
return m_LoadCombinationMap;
}
}
///
/// Store all load combination formula names
///
public List FormulaMap
{
get
{
return m_formulaMap;
}
}
///
/// Store all load usage
///
public List UsageMap
{
get
{
return m_usageMap;
}
}
#endregion
#region Methods
///
/// Default constructor of Loads
///
public Loads()
{
m_usageNameList = new List();
m_combinationNameList = new List();
m_LoadCombinationMap = new List();
m_loadUsageList = new List();
m_formulaMap = new List();
m_usageMap = new List();
m_loadCasesCategory = new List();
m_loadCases = new List();
m_loadNatures = new List();
m_loadCasesMap = new List();
m_loadNaturesMap = new List();
}
///
/// Implement this method as an external command for Revit.
///
/// An object that is passed to the external application
/// which contains data related to the command,
/// such as the application object and active view.
/// A message that can be set by the external application
/// which will be displayed if a failure or cancellation is returned by
/// the external command.
/// A set of elements to which the external application
/// can add elements that are to be highlighted in case of failure or cancellation.
/// Return the status of the external command.
/// A result of Succeeded means that the API external method functioned as expected.
/// Cancelled can be used to signify that the user cancelled the external operation
/// at some point. Failure should be returned if the application is unable to proceed with
/// the operation.
public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData,
ref string message, Autodesk.Revit.DB.ElementSet elements)
{
m_revit = commandData.Application.Application;
Transaction documentTransaction = new Transaction(commandData.Application.ActiveUIDocument.Document, "Document");
documentTransaction.Start();
// Initialize the helper classes.
m_combinationDeal = new LoadCombinationDeal(this);
m_loadCaseDeal = new LoadCaseDeal(this);
// Prepare some data for the form displaying
PrepareData();
// Display the form and wait for the user's operate.
// This class give some public methods to add or delete LoadUsage and delete LoadCombination
// The form will use these methods to add or delete dynamically.
// If the user press cancel button, return Cancelled to roll back All the changes.
using (LoadsForm displayForm = new LoadsForm(this))
{
if (DialogResult.OK != displayForm.ShowDialog())
{
documentTransaction.RollBack();
return Autodesk.Revit.UI.Result.Cancelled;
}
}
// If everything goes right, return succeeded.
documentTransaction.Commit();
return Autodesk.Revit.UI.Result.Succeeded;
}
///
/// Prepare the data for the form displaying.
///
void PrepareData()
{
// Prepare the data of the LoadCase page on form
m_loadCaseDeal.PrepareData();
//Prepare the data of the LoadCombination page on form
m_combinationDeal.PrepareData();
}
///
/// Create new Load Combination
///
/// The new Load Combination name
/// The index of new Load Combination Type
/// The index of new Load Combination State
/// true if the creation was successful; otherwise, false
public Boolean NewLoadCombination(String name, int typeId, int stateId)
{
// In order to refresh the combination DataGridView,
// We should do like as follow
m_LoadCombinationMap = new List(m_LoadCombinationMap);
// Just go to run NewLoadCombination method of LoadCombinationDeal class
return m_combinationDeal.NewLoadCombination(name, typeId, stateId);
}
///
/// Delete the selected Load Combination
///
/// The selected index in the DataGridView
/// true if the delete operation was successful; otherwise, false
public Boolean DeleteCombination(int index)
{
// Just go to run DeleteCombination method of LoadCombinationDeal class
return m_combinationDeal.DeleteCombination(index);
}
///
/// Create a new load combination usage
///
/// The new Load Usage name
/// true if the process is successful; otherwise, false
public Boolean NewLoadUsage(String usageName)
{
// In order to refresh the usage DataGridView,
// We should do like as follow
m_usageMap = new List(m_usageMap);
// Just go to run NewLoadUsage method of LoadCombinationDeal class
return m_combinationDeal.NewLoadUsage(usageName);
}
///
/// Delete the selected Load Usage
///
/// The selected index in the DataGridView
/// true if the delete operation was successful; otherwise, false
public Boolean DeleteUsage(int index)
{
// Just go to run DeleteUsage method of LoadCombinationDeal class
if (false == m_combinationDeal.DeleteUsage(index))
{
return false;
}
// In order to refresh the usage DataGridView,
// We should do like as follow
if (0 == m_usageMap.Count)
{
m_usageMap = new List();
}
return true;
}
///
/// Change usage name when the user modify it on the form
///
/// The name before modification
/// The name after modification
/// true if the modification was successful; otherwise, false
public Boolean ModifyUsageName(String oldName, String newName)
{
// Just go to run ModifyUsageName method of LoadCombinationDeal class
return m_combinationDeal.ModifyUsageName(oldName, newName);
}
///
/// Add a formula when the user click Add button to new a formula
///
/// true if the creation is successful; otherwise, false
public Boolean AddFormula()
{
// Get the first member in LoadCases as the Case
LoadCase loadCase = m_loadCases[0];
if (null == loadCase)
{
m_errorInformation = "Can't not find a LoadCase.";
return false;
}
String caseName = loadCase.Name;
// In order to refresh the formula DataGridView,
// We should do like as follow
m_formulaMap = new List(m_formulaMap);
// Run AddFormula method of LoadCombinationDeal class
return m_combinationDeal.AddFormula(caseName);
}
///
/// Delete the selected Load Formula
///
/// The selected index in the DataGridView
/// true if the delete operation was successful; otherwise, false
public Boolean DeleteFormula(int index)
{
// Just remove that data.
try
{
m_formulaMap.RemoveAt(index);
}
catch (Exception e)
{
m_errorInformation = e.ToString();
return false;
}
return true;
}
#endregion
}
}