Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:rhinocommonsamples:replacehatchpattern [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: Replacing a Hatch Object'​s Pattern =====
 +
 +===== C# =====
 +<code c#>
 +public class ReplaceHatchPatternCommand : Rhino.Commands.Command
 +{
 +  public override string EnglishName
 +  {
 +    get { return "​csReplaceHatchPattern";​ }
 +  }
 +
 +  protected override Result RunCommand(RhinoDoc doc, RunMode mode)
 +  {
 +    ObjRef[] obj_refs;
 +    var rc = RhinoGet.GetMultipleObjects("​Select hatches to replace", ​
 +      false, ObjectType.Hatch,​ out obj_refs);
 +    if (rc != Result.Success || obj_refs == null)
 +      return rc;
 +
 +    var gs = new GetString();​
 +    gs.SetCommandPrompt("​Name of replacement hatch pattern"​);​
 +    gs.AcceptNothing(false);​
 +    gs.Get();
 +    if (gs.CommandResult() != Result.Success)
 +      return gs.CommandResult();​
 +    var hatch_name = gs.StringResult();​
 +
 +    var pattern_index = doc.HatchPatterns.Find(hatch_name,​ true);
 +
 +    if (pattern_index < 0)
 +    {
 +      RhinoApp.WriteLine(
 +          "The hatch pattern \"​{0}\"​ not found  in the document.",​ hatch_name);​
 +      return Result.Nothing;​
 +    }
 +
 +    foreach (var obj_ref in obj_refs)
 +    {
 +      var hatch_object = obj_ref.Object() as HatchObject;​
 +      if (hatch_object.HatchGeometry.PatternIndex != pattern_index)
 +      {
 +        hatch_object.HatchGeometry.PatternIndex = pattern_index;​
 +        hatch_object.CommitChanges();​
 +      }
 +    }
 +    doc.Views.Redraw();​
 +    return Result.Success;​
 +  }
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +Public Class ReplaceHatchPatternCommand
 +  Inherits Rhino.Commands.Command
 +  Public Overrides ReadOnly Property EnglishName() As String
 +    Get
 +      Return "​vbReplaceHatchPattern"​
 +    End Get
 +  End Property
 +
 +  Protected Overrides Function RunCommand(doc As RhinoDoc, mode As RunMode) As Result
 +    Dim obj_refs As ObjRef()
 +    Dim rc = RhinoGet.GetMultipleObjects("​Select hatches to replace",​ False, ​
 +      ObjectType.Hatch,​ obj_refs)
 +    If rc <> Result.Success OrElse obj_refs Is Nothing Then
 +      Return rc
 +    End If
 +
 +    Dim gs = New GetString()
 +    gs.SetCommandPrompt("​Name of replacement hatch pattern"​)
 +    gs.AcceptNothing(False)
 +    gs.[Get]()
 +    If gs.CommandResult() <> Result.Success Then
 +      Return gs.CommandResult()
 +    End If
 +    Dim hatch_name = gs.StringResult()
 +
 +    Dim pattern_index = doc.HatchPatterns.Find(hatch_name,​ True)
 +
 +    If pattern_index < 0 Then
 +      RhinoApp.WriteLine(
 +          "The hatch pattern ""​{0}""​ not found  in the document.",​ hatch_name)
 +      Return Result.[Nothing]
 +    End If
 +
 +    For Each obj_ref As ObjRef In obj_refs
 +      Dim hatch_object = TryCast(obj_ref.[Object](),​ HatchObject)
 +      If hatch_object.HatchGeometry.PatternIndex <> pattern_index Then
 +        hatch_object.HatchGeometry.PatternIndex = pattern_index
 +        hatch_object.CommitChanges()
 +      End If
 +    Next
 +    doc.Views.Redraw()
 +    Return Result.Success
 +  End Function
 +End Class
 +d Namespace
 +</​code>​
 +===== Python =====
 +<code python>
 +from Rhino import *
 +from Rhino.DocObjects import *
 +from Rhino.Commands import *
 +from Rhino.Input import *
 +from Rhino.Input.Custom import *
 +from scriptcontext import doc
 +
 +def RunCommand():​
 +  rc, obj_refs = RhinoGet.GetMultipleObjects(
 +      "​Select hatches to replace",​ False, ObjectType.Hatch)
 +  if rc <> Result.Success or obj_refs == None:
 +    return rc
 +
 +  gs = GetString()
 +  gs.SetCommandPrompt("​Name of replacement hatch pattern"​)
 +  gs.AcceptNothing(False)
 +  gs.Get()
 +  if gs.CommandResult() <> Result.Success:​
 +    return gs.CommandResult()
 +  hatch_name = gs.StringResult()
 +
 +  pattern_index = doc.HatchPatterns.Find(hatch_name,​ True)
 +
 +  if pattern_index < 0:
 +    RhinoApp.WriteLine(
 +        "The hatch pattern \"​{0}\"​ not found  in the document.",​ hatch_name)
 +    return Result.Nothing
 +
 +  for obj_ref in obj_refs:
 +    hatch_object = obj_ref.Object()
 +    if hatch_object.HatchGeometry.PatternIndex <> pattern_index:​
 +      hatch_object.HatchGeometry.PatternIndex = pattern_index
 +      hatch_object.CommitChanges()
 +
 +  doc.Views.Redraw()
 +  return Result.Success
 +
 +if __name__ == "​__main__":​
 +  RunCommand()
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/replacehatchpattern.txt ยท Last modified: 2015/09/14 (external edit)