//
// (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 Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.UI;
using System.Linq;
using Autodesk.AdvanceSteel.CADAccess;
using Autodesk.Revit.DB.Steel;
using System;
using RvtDocument = Autodesk.Revit.DB.Document;
using Autodesk.AdvanceSteel.DocumentManagement;
using ASDocument = Autodesk.AdvanceSteel.DocumentManagement.Document;
using Autodesk.AdvanceSteel.Geometry;
using Autodesk.AdvanceSteel.Modelling;
namespace Utilities
{
///
/// Various useful functions
///
public static class Functions
{
///
/// Feet to mm factor
///
public static double FEET_TO_MM = 304.8;
///
/// Structural connection selection function. It uses a customizable filter, so the user can only select connections
///
public static StructuralConnectionHandler SelectConnection(UIDocument document)
{
StructuralConnectionHandler conn = null;
// Create a filter for structural connections.
LogicalOrFilter types = new LogicalOrFilter(new List { new ElementCategoryFilter(BuiltInCategory.OST_StructConnections) });
StructuralConnectionSelectionFilter filter = new StructuralConnectionSelectionFilter(types);
Reference target = document.Selection.PickObject(ObjectType.Element, filter, "Select connection element :");
if (target != null)
{
Element targetElement = document.Document.GetElement(target);
if (targetElement != null)
{
conn = targetElement as StructuralConnectionHandler;
}
}
return conn;
}
///
/// Structural connection elements selection function. It uses a customizable filter, so the user can only select allowed elements
///
public static List SelectConnectionElements(UIDocument document, string prompt)
{
List elemIds = new List();
// Create a filter for the allowed structural connection inputs.
LogicalOrFilter connElemTypes = new LogicalOrFilter(new List{
new ElementCategoryFilter(BuiltInCategory.OST_StructuralFraming),
new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumns),
new ElementCategoryFilter(BuiltInCategory.OST_StructuralFoundation),
new ElementCategoryFilter(BuiltInCategory.OST_Floors),
new ElementCategoryFilter(BuiltInCategory.OST_Walls),
new ElementCategoryFilter(BuiltInCategory.OST_StructConnectionPlates)});
StructuralConnectionSelectionFilter elemFilter = new StructuralConnectionSelectionFilter(connElemTypes);
List refs = document.Selection.PickObjects(ObjectType.Element, elemFilter, prompt).ToList();
elemIds = refs.Select(e => e.ElementId).ToList();
return elemIds;
}
///
/// Custom connection elements selection function. It uses a customizable filter, so the user can only select allowed elements
///
public static List SelectConnectionElementsCustom(UIDocument document)
{
List elemIds = new List();
// Create a filter for the allowed structural connection inputs.
LogicalOrFilter connElemTypes = new LogicalOrFilter(new List{
new ElementCategoryFilter(BuiltInCategory.OST_StructuralFraming),
new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumns),
new ElementCategoryFilter(BuiltInCategory.OST_StructConnectionBolts),
new ElementCategoryFilter(BuiltInCategory.OST_StructConnectionHoles),
new ElementCategoryFilter(BuiltInCategory.OST_StructConnectionAnchors),
new ElementCategoryFilter(BuiltInCategory.OST_StructConnectionShearStuds),
new ElementCategoryFilter(BuiltInCategory.OST_StructConnectionWelds),
new ElementCategoryFilter(BuiltInCategory.OST_StructConnectionModifiers),
new ElementCategoryFilter(BuiltInCategory.OST_StructConnectionPlates)});
StructuralConnectionSelectionFilter elemFilter = new StructuralConnectionSelectionFilter(connElemTypes);
List refs = document.Selection.PickObjects(ObjectType.Element, elemFilter, "Select elements to add to connection :").ToList();
return refs;
}
///
/// Function that returns the AS Filer Object from the selected reference object
///
public static FilerObject GetFilerObject(RvtDocument doc, Reference eRef)
{
FilerObject filerObject = null;
ASDocument curDocAS = DocumentManager.GetCurrentDocument();
if (null != curDocAS)
{
OpenDatabase currentDatabase = curDocAS.CurrentDatabase;
if (null != currentDatabase)
{
Guid uid = SteelElementProperties.GetFabricationUniqueID(doc, eRef);
string asHandle = currentDatabase.getUidDictionary().GetHandle(uid);
filerObject = FilerObject.GetFilerObjectByHandle(asHandle);
}
}
return filerObject;
}
///
/// Function that returns an AS Filer Object list from the selected reference list
///
public static List GetFilerObjectList(RvtDocument doc, IList eRefList)
{
List filerObjectList = new List();
foreach (Reference eRef in eRefList)
{
FilerObject fo = GetFilerObject(doc, eRef);
if (fo != null)
{
filerObjectList.Add(fo);
}
}
return filerObjectList;
}
///
/// Function that computes the end of a beam. Useful in applying modifiers to beams
///
public static Beam.eEnd CalculateBeamEnd(Beam asBeam, XYZ pickPoint)
{
Beam.eEnd end = Beam.eEnd.kStart;
Point3d pickPnt3d = XYZtoPoint3d(pickPoint);
Point3d startPoint = asBeam.GetPointAtStart();
Point3d endPoint = asBeam.GetPointAtEnd();
if (pickPnt3d.DistanceTo(startPoint) > pickPnt3d.DistanceTo(endPoint))
{
end = Beam.eEnd.kEnd;
}
return end;
}
///
/// Function that computes the side of a beam. Useful in applying modifiers to beams
///
public static Beam.eSide CalculateBeamSide(Beam asBeam, Autodesk.Revit.DB.XYZ pickPoint)
{
Point3d origin;
Vector3d xAxis, yAxis, zAxis;
asBeam.PhysCSStart.GetCoordSystem(out origin, out xAxis, out yAxis, out zAxis);
Point3d pickPnt3d = XYZtoPoint3d(pickPoint);
asBeam.SetUpDownTag(pickPnt3d, Beam.eTag.kThisSide);
return asBeam.GetTaggedUpDown(Beam.eTag.kThisSide);
}
///
/// Function that computes the end of a beam. Useful in applying modifiers to beams
///
public static Point3d XYZtoPoint3d(XYZ p)
{
Point3d result = new Point3d(0, 0, 0);
result.x = FEET_TO_MM * p.X;
result.y = FEET_TO_MM * p.Y;
result.z = FEET_TO_MM * p.Z;
return result;
}
}
//implementation of ISelectionFilter. Needed for connection deletion
class StructuralConnectionSelectionFilter : ISelectionFilter
{
LogicalOrFilter _filter;
///
/// Initialize the filter with the accepted element types.
///
/// Logical filter containing accepted element types.
///
public StructuralConnectionSelectionFilter(LogicalOrFilter elemTypesAllowed)
{
_filter = elemTypesAllowed;
}
///
/// Allows an element to be selected
///
/// A candidate element in the selection operation.
/// Return true to allow the user to select this candidate element.
public bool AllowElement(Element element)
{
return _filter.PassesFilter(element);
}
///
/// Allows a reference to be selected.
///
/// A candidate reference in the selection operation.
/// The 3D position of the mouse on the candidate reference.
/// Return true to allow the user to select this candidate reference.
public bool AllowReference(Reference refer, XYZ point)
{
return true;
}
}
}