-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* HighlightElementsComponent * Boolean toggle input to clear highlight and DocumentContextChanged eventhandler * Rename Clear input to Enabled and use AddBooleanParameter with default true * Minor changes and OnParameterSourcesChanged eventhandler to clear highlight when input was removed * Change the order of parameters. * Fix API error of using transparency from the non-highlighted color. * Merge from main. * Minor modifications. --------- Co-authored-by: Viktor Kovacs <[email protected]>
- Loading branch information
Showing
7 changed files
with
240 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
...plugin/TapirGrasshopperPlugin/Components/ElementsComponents/HighlightElementsComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
using Grasshopper.Kernel; | ||
using Grasshopper.Kernel.Types; | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
using TapirGrasshopperPlugin.Data; | ||
using TapirGrasshopperPlugin.Utilities; | ||
using System.Windows.Forms; | ||
using System.Xml.Linq; | ||
|
||
namespace TapirGrasshopperPlugin.Components.ElementsComponents | ||
{ | ||
|
||
public class HighlightElementsComponent : ArchicadAccessorComponent | ||
{ | ||
public HighlightElementsComponent () | ||
: base ( | ||
"Highlight Elements", | ||
"HighlightElems", | ||
"Highlight Elements.", | ||
"Elements" | ||
) | ||
{ | ||
Params.ParameterSourcesChanged += OnParameterSourcesChanged; | ||
} | ||
|
||
public override void DocumentContextChanged (GH_Document document, GH_DocumentContext context) | ||
{ | ||
base.DocumentContextChanged (document, context); | ||
if (context == GH_DocumentContext.Close || context == GH_DocumentContext.Unloaded) { | ||
ClearHighlight (); | ||
} | ||
} | ||
|
||
public override void RemovedFromDocument (GH_Document document) | ||
{ | ||
base.RemovedFromDocument (document); | ||
ClearHighlight (); | ||
} | ||
|
||
public override void AddedToDocument (GH_Document document) | ||
{ | ||
int counter = 0; | ||
GH_Document doc = OnPingDocument (); | ||
if (doc != null) { | ||
foreach (IGH_DocumentObject obj in doc.Objects) | ||
if (obj.ComponentGuid == ComponentGuid) { | ||
counter++; | ||
if (counter > 1) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (counter > 1) { | ||
doc.RemoveObject (this, true); | ||
MessageBox.Show ("Only one instance can be created.", "Highlight Elements"); | ||
} else { | ||
base.AddedToDocument (document); | ||
} | ||
} | ||
|
||
protected override void RegisterInputParams (GH_InputParamManager pManager) | ||
{ | ||
pManager.AddBooleanParameter ("Enable", "Enable", "Enable highlight.", GH_ParamAccess.item, @default: true); | ||
pManager.AddGenericParameter ("ElementIds", "ElementIds", "Elements to highlight.", GH_ParamAccess.list); | ||
pManager.AddColourParameter ("HighligtedColors", "Colors", "Colors for the elements.", GH_ParamAccess.list); | ||
pManager.AddColourParameter ("NonHighligtedColor", "NHColor", "Color for the non-highlighted elements.", GH_ParamAccess.item); | ||
pManager.AddBooleanParameter ("NonHighligtedWireframe", "NHWire3D", "Switch non-highlighted elements in the 3D window to wireframe", GH_ParamAccess.item, @default: false); | ||
pManager.AddNumberParameter ("Transparency", "Transparency", "Sets the transparency of the highlight (0.0: opaque, 1.0: transparent).", GH_ParamAccess.item, @default: 0.0); | ||
} | ||
|
||
protected override void RegisterOutputParams (GH_OutputParamManager pManager) | ||
{ | ||
} | ||
|
||
protected override void SolveInstance (IGH_DataAccess DA) | ||
{ | ||
bool enabled = true; | ||
if (DA.GetData (0, ref enabled) && !enabled) { | ||
ClearHighlight (); | ||
return; | ||
} | ||
|
||
ElementsObj inputElements = ElementsObj.Create (DA, 1); | ||
if (inputElements == null) { | ||
AddRuntimeMessage (GH_RuntimeMessageLevel.Error, "Input ElementIds failed to collect data."); | ||
return; | ||
} | ||
|
||
List<GH_Colour> highlightedColors = new List<GH_Colour> (); | ||
if (!DA.GetDataList (2, highlightedColors)) { | ||
return; | ||
} | ||
|
||
if (highlightedColors.Count != 1 && inputElements.Elements.Count != highlightedColors.Count) { | ||
AddRuntimeMessage (GH_RuntimeMessageLevel.Error, "The count of highlighted colors must be 1 or the same as the count of ElementIds."); | ||
return; | ||
} | ||
|
||
GH_Colour nonHighlightedColor = new GH_Colour (); | ||
if (!DA.GetData (3, ref nonHighlightedColor)) { | ||
return; | ||
} | ||
|
||
bool wireframe3D = false; | ||
if (!DA.GetData (4, ref wireframe3D)) { | ||
return; | ||
} | ||
|
||
double transparency = 0.0; | ||
if (!DA.GetData<double> (5, ref transparency)) { | ||
return; | ||
} | ||
if (transparency < 0.0) { | ||
transparency = 0.0; | ||
} else if (transparency > 1.0) { | ||
transparency = 1.0; | ||
} | ||
|
||
// There is an error in the Archicad API implementation: the transparency | ||
// always comes from the non-highlighted element color. | ||
HighlightElementsObj highlightElements = new HighlightElementsObj () { | ||
Elements = inputElements.Elements, | ||
HighlightedColors = Utilities.Convert.ToRGBColors (highlightedColors, 255, inputElements.Elements.Count), | ||
NonHighlightedColor = Utilities.Convert.ToRGBColor (nonHighlightedColor, System.Convert.ToInt32 (transparency * 255.0)), | ||
Wireframe3D = wireframe3D | ||
}; | ||
JObject highlightElementsObj = JObject.FromObject (highlightElements); | ||
CommandResponse response = SendArchicadAddOnCommand ("TapirCommand", "HighlightElements", highlightElementsObj); | ||
if (!response.Succeeded) { | ||
AddRuntimeMessage (GH_RuntimeMessageLevel.Error, response.GetErrorMessage ()); | ||
return; | ||
} | ||
} | ||
|
||
private void ClearHighlight () | ||
{ | ||
HighlightElementsObj highlightElements = new HighlightElementsObj (); | ||
JObject highlightElementsObj = JObject.FromObject (highlightElements); | ||
CommandResponse response = SendArchicadAddOnCommand ("TapirCommand", "HighlightElements", highlightElementsObj); | ||
if (!response.Succeeded) { | ||
AddRuntimeMessage (GH_RuntimeMessageLevel.Error, response.GetErrorMessage ()); | ||
return; | ||
} | ||
} | ||
|
||
public void OnParameterSourcesChanged (object source, EventArgs e) | ||
{ | ||
foreach (IGH_Param param in Params.Input) { | ||
if (!param.Optional && param.SourceCount == 0) { | ||
ClearHighlight (); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
protected override System.Drawing.Bitmap Icon => TapirGrasshopperPlugin.Properties.Resources.HighlightElems; | ||
|
||
public override Guid ComponentGuid => new Guid ("9ba098dd-63c5-4126-b4cc-3caa56082c8f"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
grasshopper-plugin/TapirGrasshopperPlugin/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
grasshopper-plugin/TapirGrasshopperPlugin/Utilities/DataUtilities.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Grasshopper.Kernel.Types; | ||
|
||
namespace TapirGrasshopperPlugin.Utilities | ||
{ | ||
public class Convert | ||
{ | ||
static public List<int> ToRGBColor (GH_Colour colorRGBA, int alpha) | ||
{ | ||
if (colorRGBA == null) { | ||
return null; | ||
} | ||
return new List<int> { | ||
colorRGBA.Value.R, | ||
colorRGBA.Value.G, | ||
colorRGBA.Value.B, | ||
alpha | ||
}; | ||
} | ||
|
||
static public List<List<int>> ToRGBColors (List<GH_Colour> colorRGBAs, int alpha, int minSize) | ||
{ | ||
if (colorRGBAs == null) { | ||
return null; | ||
} | ||
List<List<int>> colors = new List<List<int>> (); | ||
foreach (GH_Colour colorRGBA in colorRGBAs) { | ||
colors.Add (ToRGBColor (colorRGBA, alpha)); | ||
} | ||
for (int i = colors.Count; i < minSize; ++i) { | ||
colors.Add (colors.Last ()); | ||
} | ||
return colors; | ||
} | ||
} | ||
} |