-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
320cedd
commit 8ec81c5
Showing
7 changed files
with
136 additions
and
1 deletion.
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
67 changes: 67 additions & 0 deletions
67
rhinocommon/cs/SampleCsCommands/SampleCsPerFaceMaterial.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,67 @@ | ||
using System; | ||
using System.Security.Cryptography; | ||
using Rhino; | ||
using Rhino.Commands; | ||
using Rhino.DocObjects; | ||
using Rhino.Input.Custom; | ||
|
||
namespace SampleCsCommands | ||
{ | ||
public class SampleCsPerFaceMaterial : Command | ||
{ | ||
public override string EnglishName => "SampleCsPerFaceMaterial"; | ||
|
||
protected override Result RunCommand(RhinoDoc doc, RunMode mode) | ||
{ | ||
var render_plugin_id = Rhino.Render.Utilities.DefaultRenderPlugInId; | ||
var idi = new GuidIndex(); | ||
|
||
var go = new GetObject(); | ||
go.SetCommandPrompt("Select object with the rendering material you want to assign"); | ||
go.EnablePreSelect(false, true); | ||
go.EnablePostSelect(true); | ||
go.Get(); | ||
if (go.CommandResult() != Result.Success) | ||
return go.CommandResult(); | ||
|
||
var obj = go.Object(0).Object(); | ||
if (null == obj) | ||
return Result.Failure; | ||
|
||
var att = obj.Attributes; | ||
if ((idi.Index = att.MaterialIndex) >= 0) | ||
{ | ||
idi.Id = doc.Materials[idi.Index].Id; | ||
} | ||
else | ||
{ | ||
MaterialRef mat_ref = null; | ||
if (att.MaterialRefs.ContainsKey(idi.Id)) | ||
mat_ref = att.MaterialRefs[render_plugin_id]; | ||
if (null == mat_ref) | ||
mat_ref = att.MaterialRefs[Guid.Empty]; | ||
if (null != mat_ref) | ||
{ | ||
idi.Id = mat_ref.FrontFaceMaterialId; | ||
idi.Index = doc.Materials.Find(idi.Id, true); | ||
} | ||
} | ||
|
||
if (!idi.IsValid) | ||
{ | ||
RhinoApp.WriteLine("This object does not have a rendering material."); | ||
return Result.Nothing; | ||
} | ||
|
||
return Result.Success; | ||
} | ||
} | ||
|
||
internal class GuidIndex | ||
{ | ||
public Guid Id { get; set; } = Guid.Empty; | ||
public int Index { get; set; } = RhinoMath.UnsetIntIndex; | ||
|
||
public bool IsValid => Id != Guid.Empty && Index >= 0; | ||
} | ||
} |
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,67 @@ | ||
using Rhino; | ||
using Rhino.Commands; | ||
using Rhino.Input.Custom; | ||
|
||
namespace SampleCsCommands | ||
{ | ||
public class SampleCsSetObjectName : Command | ||
{ | ||
public override string EnglishName => "SampleCsSetObjectName"; | ||
|
||
protected override Result RunCommand(RhinoDoc doc, RunMode mode) | ||
{ | ||
var go = new GetObject(); | ||
go.SetCommandPrompt("Select objects"); | ||
go.SubObjectSelect = false; | ||
go.ReferenceObjectSelect = false; | ||
go.GetMultiple(1, 0); | ||
if (go.CommandResult() != Result.Success) | ||
return go.CommandResult(); | ||
|
||
string defaultName = null; | ||
foreach (var objRef in go.Objects()) | ||
{ | ||
var rhObj = objRef.Object(); | ||
if (null == rhObj) | ||
return Result.Failure; | ||
|
||
if (string.IsNullOrEmpty(defaultName)) | ||
defaultName = rhObj.Attributes.Name; | ||
else if (!defaultName.Equals(rhObj.Attributes.Name)) | ||
{ | ||
defaultName = "varies"; | ||
break; | ||
} | ||
} | ||
|
||
var gs = new GetString(); | ||
gs.SetCommandPrompt("Object name"); | ||
gs.SetDefaultString(defaultName); | ||
gs.Get(); | ||
if (gs.CommandResult() != Result.Success) | ||
return gs.CommandResult(); | ||
|
||
var newName = gs.StringResult(); | ||
newName = newName.Trim(); | ||
|
||
if (defaultName.Equals(newName)) | ||
return Result.Nothing; | ||
|
||
foreach (var objRef in go.Objects()) | ||
{ | ||
var rhObj = objRef.Object(); | ||
if (null == rhObj) | ||
return Result.Failure; | ||
|
||
if (!newName.Equals(rhObj.Attributes.Name)) | ||
{ | ||
var attributes = rhObj.Attributes.Duplicate(); | ||
attributes.Name = newName; | ||
doc.Objects.ModifyAttributes(rhObj, attributes, false); | ||
} | ||
} | ||
|
||
return Result.Success; | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.