//
// (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 Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit;
using Autodesk.Revit.DB.Architecture;
using System.Drawing.Design;
using System.ComponentModel;
namespace Revit.SDK.Samples.NewHostedSweep.CS
{
///
/// This class contains the data for hosted sweep modification.
///
public class ModificationData
{
///
/// Element to modify.
///
private HostedSweep m_elemToModify;
///
/// Creation data can be modified.
///
private CreationData m_creationData;
///
/// Revit active document.
///
private Document m_rvtDoc;
///
/// Revit UI document.
///
private UIDocument m_rvtUIDoc;
///
/// Sub transaction
///
Transaction m_transaction;
///
/// Constructor with HostedSweep and CreationData as parameters.
///
/// Element to modify
/// CreationData
public ModificationData(HostedSweep elem, CreationData creationData)
{
m_rvtDoc = creationData.Creator.RvtDocument;
m_rvtUIDoc = creationData.Creator.RvtUIDocument;
m_elemToModify = elem;
m_creationData = creationData;
m_transaction = new Transaction(m_rvtDoc, "External Tool");
m_creationData.EdgeAdded +=
new CreationData.EdgeEventHandler(m_creationData_EdgeAdded);
m_creationData.EdgeRemoved +=
new CreationData.EdgeEventHandler(m_creationData_EdgeRemoved);
m_creationData.SymbolChanged +=
new CreationData.SymbolChangedEventHandler(m_creationData_SymbolChanged);
}
///
/// Name of the Creator.
///
public string CreatorName
{
get
{
return m_creationData.Creator.Name;
}
}
///
/// Change the symbol of the HostedSweep.
///
///
private void m_creationData_SymbolChanged(ElementType sym)
{
try
{
StartTransaction();
m_elemToModify.ChangeTypeId(sym.Id);
CommitTransaction();
}
catch
{
RollbackTransaction();
}
}
///
/// Remove the edge from the HostedSweep.
///
///
private void m_creationData_EdgeRemoved(Edge edge)
{
try
{
StartTransaction();
m_elemToModify.RemoveSegment(edge.Reference);
CommitTransaction();
}
catch
{
RollbackTransaction();
}
}
///
/// Add the edge to the HostedSweep.
///
///
private void m_creationData_EdgeAdded(Edge edge)
{
try
{
StartTransaction();
if (m_elemToModify is Fascia)
{
(m_elemToModify as Fascia).AddSegment(edge.Reference);
}
else if (m_elemToModify is Gutter)
{
(m_elemToModify as Gutter).AddSegment(edge.Reference);
}
else if (m_elemToModify is SlabEdge)
{
(m_elemToModify as SlabEdge).AddSegment(edge.Reference);
}
CommitTransaction();
}
catch
{
RollbackTransaction();
}
}
///
/// Show the element in a good view.
///
public void ShowElement()
{
try
{
StartTransaction();
m_rvtUIDoc.ShowElements(m_elemToModify);
CommitTransaction();
}
catch
{
RollbackTransaction();
}
}
///
/// Name will be displayed in property grid.
///
[Category("Identity Data")]
public String Name
{
get
{
String result = "[Id:" + m_elemToModify.Id.IntegerValue + "] ";
return result + m_elemToModify.Name;
}
}
///
/// HostedSweep Angle property.
///
[Category("Profile")]
public String Angle
{
get
{
Parameter angle = GetParameter("Angle");
if (angle != null)
return angle.AsValueString();
else
return m_elemToModify.Angle.ToString();
}
set
{
try
{
StartTransaction();
Parameter angle = GetParameter("Angle");
if (angle != null)
angle.SetValueString(value);
else
m_elemToModify.Angle = double.Parse(value);
CommitTransaction();
}
catch
{
RollbackTransaction();
}
}
}
///
/// HostedSweep profiles edges, the edges can be removed or added in the
/// pop up dialog.
///
[TypeConverter(typeof(CreationDataTypeConverter)),
Editor(typeof(EdgeFormUITypeEditor), typeof(UITypeEditor)),
Category("Profile"), DisplayName("Profile Edges")]
public CreationData AddOrRemoveSegments
{
get
{
return m_creationData;
}
}
///
/// HostedSweep Length property.
///
[Category("Dimensions")]
public string Length
{
get
{
Parameter length = GetParameter("Length");
if (length != null)
return length.AsValueString();
else
return m_elemToModify.Length.ToString();
}
}
///
/// HostedSweep HorizontalFlipped property.
///
[Category("Constraints"), DisplayName("Horizontal Profile Flipped")]
public bool HorizontalFlipped
{
get { return m_elemToModify.HorizontalFlipped; }
set
{
if (value != m_elemToModify.HorizontalFlipped)
{
try
{
StartTransaction();
m_elemToModify.HorizontalFlip();
CommitTransaction();
}
catch
{
RollbackTransaction();
}
}
}
}
///
/// HostedSweep HorizontalOffset property.
///
[Category("Constraints"), DisplayName("Horizontal Profile Offset")]
public String HorizontalOffset
{
get
{
Parameter horiOff = GetParameter("Horizontal Profile Offset");
if (horiOff != null)
return horiOff.AsValueString();
else
return m_elemToModify.HorizontalOffset.ToString();
}
set
{
try
{
StartTransaction();
Parameter horiOff = GetParameter("Horizontal Profile Offset");
if (horiOff != null)
horiOff.SetValueString(value);
else
m_elemToModify.HorizontalOffset = double.Parse(value);
CommitTransaction();
}
catch
{
RollbackTransaction();
}
}
}
///
/// HostedSweep VerticalFlipped property.
///
[Category("Constraints"), DisplayName("Vertical Profile Flipped")]
public bool VerticalFlipped
{
get
{
return m_elemToModify.VerticalFlipped;
}
set
{
if (value != m_elemToModify.VerticalFlipped)
{
try
{
StartTransaction();
m_elemToModify.VerticalFlip();
CommitTransaction();
}
catch
{
RollbackTransaction();
}
}
}
}
///
/// HostedSweep VerticalOffset property.
///
[Category("Constraints"), DisplayName("Vertical Profile Offset")]
public String VerticalOffset
{
get
{
Parameter vertOff = GetParameter("Vertical Profile Offset");
if (vertOff != null)
return vertOff.AsValueString();
else
return m_elemToModify.VerticalOffset.ToString();
}
set
{
try
{
StartTransaction();
Parameter vertOff = GetParameter("Vertical Profile Offset");
if (vertOff != null)
vertOff.SetValueString(value);
else
m_elemToModify.VerticalOffset = double.Parse(value);
CommitTransaction();
}
catch
{
RollbackTransaction();
}
}
}
///
/// Get parameter by given name.
///
/// name of parameter
/// parameter whose definition name is the given name.
protected Parameter GetParameter(String name)
{
return m_elemToModify.LookupParameter(name);
}
public TransactionStatus StartTransaction()
{
return m_transaction.Start();
}
public TransactionStatus CommitTransaction()
{
return m_transaction.Commit();
}
public TransactionStatus RollbackTransaction()
{
return m_transaction.RollBack();
}
}
}