Skip to content

Commit

Permalink
Add SampleCsParseTextFields
Browse files Browse the repository at this point in the history
  • Loading branch information
travisserio committed Jun 1, 2020
1 parent c176136 commit c209445
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
88 changes: 88 additions & 0 deletions rhinocommon/cs/SampleCsCommands/SampleCSParseTextFields.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using Rhino;
using Rhino.Commands;
using Rhino.Input.Custom;

namespace SampleCsCommands
{
public class SampleCsParseTextFields : Command
{
static SampleCsParseTextFields _instance;
public SampleCsParseTextFields()
{
_instance = this;
}

///<summary>The only instance of the SampleCSParseTextFields command.</summary>
public static SampleCsParseTextFields Instance
{
get { return _instance; }
}

public override string EnglishName
{
get { return "SampleCSParseTextFields"; }
}

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{

//Select some objects to add attribute user text to
var go = new GetObject();
go.SetCommandPrompt("Select objects");
go.GroupSelect = true;
go.GetMultiple(1, 0);
if (go.CommandResult() != Result.Success)
return go.CommandResult();


//Apply the ObjectName textfield as the user text value to the selected objects
for (var i = 0; i < go.ObjectCount; i++)
{
var obj_ref = go.Object(i);
var obj = obj_ref.Object();
if (null != obj)
{
//Create an ObjectName TextField and apply it as the value of the user text
var fx = $"%<ObjectName(\"{obj_ref.ObjectId}\")>%";
obj.Attributes.SetUserString("ObjectName", fx);
}
}



//Now retrieve the values we just set and parse them
for (var i = 0; i < go.ObjectCount; i++)
{
var obj_ref = go.Object(i);
var obj = obj_ref.Object();
if (null == obj)
continue;

//Read user text value and parse it as a textfield
var user_string_value = obj.Attributes.GetUserString("ObjectName");
if (!string.IsNullOrEmpty(user_string_value))
{
if (user_string_value.StartsWith("%<") && user_string_value.EndsWith(">%"))
{
var parsed_string = RhinoApp.ParseTextField(user_string_value, obj_ref.Object(), null);
RhinoApp.WriteLine($"Parsed TextField: {parsed_string}");
}
}

//Direct method call to TextField ObjectName
var direct_string = Rhino.Runtime.TextFields.ObjectName(obj_ref.ObjectId.ToString());
var direct_area = Rhino.Runtime.TextFields.Area(obj_ref.ObjectId.ToString(), UnitSystem.Millimeters.ToString());
RhinoApp.WriteLine($"Direct ObjectName call: {direct_string}");
RhinoApp.WriteLine($"Direct Area call: {direct_area}");
}






return Result.Success;
}
}
}
10 changes: 5 additions & 5 deletions rhinocommon/cs/SampleCsCommands/SampleCSSilhouetteDraftCurve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@

namespace SampleCsCommands
{
public class SampleCSSilhouetteDraftCurve : Command
public class SampleCsSilhouetteDraftCurve : Command
{
static SampleCSSilhouetteDraftCurve _instance;
public SampleCSSilhouetteDraftCurve()
static SampleCsSilhouetteDraftCurve _instance;
public SampleCsSilhouetteDraftCurve()
{
_instance = this;
}

///<summary>The only instance of the SampleCSShilhouetteDraftCurve command.</summary>
public static SampleCSSilhouetteDraftCurve Instance
public static SampleCsSilhouetteDraftCurve Instance
{
get { return _instance; }
}

public override string EnglishName
{
get { return "SampleCSSilhouetteDraftCurve"; }
get { return "SampleCsSilhouetteDraftCurve"; }
}

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
Expand Down
3 changes: 2 additions & 1 deletion rhinocommon/cs/SampleCsCommands/SampleCsCommands.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
<Compile Include="SampleCsOrientOnMesh.cs" />
<Compile Include="SampleCsOrientPerpendicularToCurve.cs" />
<Compile Include="SampleCsOverCut.cs" />
<Compile Include="SampleCsParseTextFields.cs" />
<Compile Include="SampleCsPersistentSettings.cs" />
<Compile Include="SampleCsPickHole.cs" />
<Compile Include="SampleCsPictureFrame.cs" />
Expand Down Expand Up @@ -200,7 +201,7 @@
<Compile Include="SampleCsSetPoint.cs" />
<Compile Include="SampleCsShadedBrep.cs" />
<Compile Include="SampleCsShadedView.cs" />
<Compile Include="SampleCSSilhouetteDraftCurve.cs" />
<Compile Include="SampleCsSilhouetteDraftCurve.cs" />
<Compile Include="SampleCsSineWaveLoft.cs" />
<Compile Include="SampleCsSketch.cs" />
<Compile Include="SampleCsSmash.cs" />
Expand Down

0 comments on commit c209445

Please sign in to comment.