Well the code works, its just when I move to a different machine importing the .dll doesn’t work. Anyways here is the whole code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.DesignScript.Runtime;
using Autodesk.DesignScript.Interfaces;
using Autodesk.DesignScript.Geometry;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using RevitServices.Persistence;
using RevitServices.Transactions;
using Revit.GeometryConversion;
///////////////////////////////////////////////////////////////////
/// NOTE: This project requires references to the DynamoServices
/// and ProtoGeometry DLLs. These can be found in the latest
/// ZeroTouch and DynamoServices Nuget packages.
///////////////////////////////////////////////////////////////////
namespace ColorElements
{
public class ColorElements
{
// Two private variables for example purposes
private double _a;
private double _b;
// We make the constructor for this object internal because the
// Dynamo user should construct an object through a static method
internal ColorElements(double a, double b)
{
_a = a;
_b = b;
}
public static List<float> createColor(float red, float green, float blue)
{
List<float> Color = new List<float>();
Color.Add(red);
Color.Add(green);
Color.Add(blue);
return Color;
}
public static string ColorElementsInViews(List<Revit.Elements.Element> elements, List<Revit.Elements.Views.View> views, Revit.Elements.FillPatternElement fillPattern, List<float> fillColor, List<float> lineColor, float lineWeight)
{
string errorReport = "";
try
{
// Create and modify graphics settings
Autodesk.Revit.DB.OverrideGraphicSettings gSettings = new OverrideGraphicSettings();
FillPatternElement unwrappedPattern = fillPattern.InternalElement as FillPatternElement;
gSettings.SetProjectionFillPatternId(unwrappedPattern.Id);
Color fill = new Color((byte)(int)fillColor[0], (byte)(int)fillColor[1], (byte)(int)fillColor[2]);
Color line = new Color((byte)(int)lineColor[0], (byte)(int)lineColor[1], (byte)(int)lineColor[2]);
gSettings.SetProjectionFillColor(fill);
gSettings.SetProjectionLineColor(line);
gSettings.SetProjectionLineWeight((int)lineWeight);
// Ensure in transaction
Document doc = DocumentManager.Instance.CurrentUIDocument.Document;
TransactionManager.Instance.EnsureInTransaction(doc);
// For every element, if in views color
foreach (Revit.Elements.Element e in elements)
{
Element unwrapped = e.InternalElement;
ElementId id = unwrapped.Id;
foreach (Revit.Elements.Views.View v in views)
{
View unwrappedView = v.InternalElement as View;
unwrappedView.SetElementOverrides(id, gSettings);
}
}
TransactionManager.Instance.TransactionTaskDone();
errorReport = "success";
}
catch(Exception c)
{
errorReport += c.ToString();
}
return errorReport;
}
public static string ColorListElementsInViews(List<List<Revit.Elements.Element>> elementList, List<Revit.Elements.Views.View> views, Revit.Elements.FillPatternElement fillPattern, List<List<float>> fillColorList, List<float> lineColor, float lineWeight)
{
string errorReport = "";
try
{
// Element List and Color List must match
if (elementList.Count != fillColorList.Count)
{
errorReport = "Element list and color list must be the same length!";
throw new Exception();
}
for(int i = 0; i < elementList.Count; i++)
{
// Create and modify graphics settings
Autodesk.Revit.DB.OverrideGraphicSettings gSettings = new OverrideGraphicSettings();
FillPatternElement unwrappedPattern = fillPattern.InternalElement as FillPatternElement;
Color fill = new Color((byte)(int)fillColorList[i][0], (byte)(int)fillColorList[i][1], (byte)(int)fillColorList[i][2]);
Color line = new Color((byte)(int)lineColor[0], (byte)(int)lineColor[1], (byte)(int)lineColor[2]);
gSettings.SetProjectionFillPatternId(unwrappedPattern.Id);
gSettings.SetProjectionFillColor(fill);
gSettings.SetProjectionLineColor(line);
gSettings.SetProjectionLineWeight((int)lineWeight);
// Ensure in transaction
Document doc = DocumentManager.Instance.CurrentUIDocument.Document;
TransactionManager.Instance.EnsureInTransaction(doc);
// For every element, if in views color
foreach (Revit.Elements.Element e in elementList[i])
{
Element unwrapped = e.InternalElement;
ElementId id = unwrapped.Id;
foreach (Revit.Elements.Views.View v in views)
{
View unwrappedView = v.InternalElement as View;
unwrappedView.SetElementOverrides(id, gSettings);
}
}
TransactionManager.Instance.TransactionTaskDone();
errorReport = "success";
}
}
catch (Exception c)
{
errorReport += c.ToString();
}
return errorReport;
}
public static string Clear(List<Revit.Elements.Element> elements, List<Revit.Elements.Views.View> views)
{
string errorReport = "";
try
{
Autodesk.Revit.DB.OverrideGraphicSettings gSettings = new OverrideGraphicSettings();
Document doc = DocumentManager.Instance.CurrentUIDocument.Document;
TransactionManager.Instance.EnsureInTransaction(doc);
// For every element, if in views color
foreach (Revit.Elements.Element e in elements)
{
Element unwrapped = e.InternalElement;
ElementId id = unwrapped.Id;
foreach (Revit.Elements.Views.View v in views)
{
View unwrappedView = v.InternalElement as View;
unwrappedView.SetElementOverrides(id, gSettings);
}
}
TransactionManager.Instance.TransactionTaskDone();
errorReport = "success";
}
catch(Exception x)
{
errorReport = x.ToString();
}
return errorReport;
}
}
}