Site Tools


Differences

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

Link to this comparison view

developer:rhinocommonsamples:renameblock [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: Rename Instance Definition (Block) =====
 +
 +===== C# =====
 +<code c#>
 +[System.Runtime.InteropServices.Guid("​76B20E41-A462-4C75-8FCC-4E07AE5E14BB"​)]
 +public class RenameBlockCommand : Command
 +{
 +  public override string EnglishName { get { return "​csRenameInstanceDefinition";​ } }
 +
 +  protected override Result RunCommand(RhinoDoc doc, RunMode mode)
 +  {
 +    // Get the name of the insance definition to rename
 +    string instanceDefinitionName = "";​
 +    var rc = RhinoGet.GetString("​Name of block to rename",​ true, ref instanceDefinitionName);​
 +    if (rc != Result.Success)
 +      return rc;
 +    if (string.IsNullOrWhiteSpace(instanceDefinitionName))
 +      return Result.Nothing;​
 +   
 +    // Verify instance definition exists
 +    var instanceDefinition = doc.InstanceDefinitions.Find(instanceDefinitionName,​ true);
 +    if (instanceDefinition == null) {
 +      RhinoApp.WriteLine(string.Format("​Block \"​{0}\"​ not found.",​ instanceDefinitionName));​
 +      return Result.Nothing;​
 +    }
 +
 +    // Verify instance definition is rename-able
 +    if (instanceDefinition.IsDeleted || instanceDefinition.IsReference) {
 +      RhinoApp.WriteLine(string.Format("​Unable to rename block \"​{0}\"​.",​ instanceDefinitionName));​
 +      return Result.Nothing;​
 +    }
 +   
 +    // Get the new instance definition name
 +    string instanceDefinitionNewName = "";​
 +    rc = RhinoGet.GetString("​Name of block to rename",​ true, ref instanceDefinitionNewName);​
 +    if (rc != Result.Success)
 +      return rc;
 +    if (string.IsNullOrWhiteSpace(instanceDefinitionNewName))
 +      return Result.Nothing;​
 +
 +    // Verify the new instance definition name is not already in use
 +    var existingInstanceDefinition = doc.InstanceDefinitions.Find(instanceDefinitionNewName,​ true);
 +    if (existingInstanceDefinition != null && !existingInstanceDefinition.IsDeleted) {
 +      RhinoApp.WriteLine(string.Format("​Block \"​{0}\"​ already exists.",​ existingInstanceDefinition));​
 +      return Result.Nothing;​
 +    }
 +   
 +    // change the block name
 +    if (!doc.InstanceDefinitions.Modify(instanceDefinition.Index,​ instanceDefinitionNewName,​ instanceDefinition.Description,​ true)) {
 +      RhinoApp.WriteLine(string.Format("​Could not rename {0} to {1}", instanceDefinition.Name,​ instanceDefinitionNewName));​
 +      return Result.Failure;​
 +    }
 +
 +    return Result.Success;​
 +  }
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +<​System.Runtime.InteropServices.Guid("​8FE26C83-3361-497D-B26A-CB2B2A9F0D56"​)>​ _
 +Public Class RenameBlockCommand
 +  Inherits Command
 +  Public Overrides ReadOnly Property EnglishName() As String
 +    Get
 +      Return "​vbRenameInstanceDefinition"​
 +    End Get
 +  End Property
 +
 +  Protected Overrides Function RunCommand(doc As RhinoDoc, mode As RunMode) As Result
 +    ' Get the name of the insance definition to rename
 +    Dim instanceDefinitionName As String = ""​
 +    Dim rc = Rhino.Input.RhinoGet.GetString("​Name of block to rename",​ True, instanceDefinitionName)
 +    If rc <> Result.Success Then
 +      Return rc
 +    End If
 +    If [String].IsNullOrWhiteSpace(instanceDefinitionName) Then
 +      Return Result.[Nothing]
 +    End If
 +
 +    ' Verify instance definition exists
 +    Dim instanceDefinition = doc.InstanceDefinitions.Find(instanceDefinitionName,​ True)
 +    If instanceDefinition Is Nothing Then
 +      RhinoApp.WriteLine([String].Format("​Block ""​{0}""​ not found.",​ instanceDefinitionName))
 +      Return Result.[Nothing]
 +    End If
 +
 +    ' Verify instance definition is rename-able
 +    If instanceDefinition.IsDeleted OrElse instanceDefinition.IsReference Then
 +      RhinoApp.WriteLine([String].Format("​Unable to rename block ""​{0}""​.",​ instanceDefinitionName))
 +      Return Result.[Nothing]
 +    End If
 +
 +    ' Get the new instance definition name
 +    Dim instanceDefinitionNewName As String = ""​
 +    rc = Rhino.Input.RhinoGet.GetString("​Name of block to rename",​ True, instanceDefinitionNewName)
 +    If rc <> Result.Success Then
 +      Return rc
 +    End If
 +    If [String].IsNullOrWhiteSpace(instanceDefinitionNewName) Then
 +      Return Result.[Nothing]
 +    End If
 +
 +    ' Verify the new instance definition name is not already in use
 +    Dim existingInstanceDefinition = doc.InstanceDefinitions.Find(instanceDefinitionNewName,​ True)
 +    If existingInstanceDefinition IsNot Nothing AndAlso Not existingInstanceDefinition.IsDeleted Then
 +      RhinoApp.WriteLine([String].Format("​Block ""​{0}""​ already exists.",​ existingInstanceDefinition))
 +      Return Result.[Nothing]
 +    End If
 +
 +    ' change the block name
 +    If Not doc.InstanceDefinitions.Modify(instanceDefinition.Index,​ instanceDefinitionNewName,​ instanceDefinition.Description,​ True) Then
 +      RhinoApp.WriteLine([String].Format("​Could not rename {0} to {1}", instanceDefinition.Name,​ instanceDefinitionNewName))
 +      Return Result.Failure
 +    End If
 +
 +    Return Result.Success
 +  End Function
 +End Class
 +d Namespace
 +</​code>​
 +===== Python =====
 +<code python>
 +import rhinoscriptsyntax as rs
 +from scriptcontext import doc
 +
 +def Rename():
 +    blockName = rs.GetString("​block to rename"​)
 +    instanceDefinition = doc.InstanceDefinitions.Find(blockName,​ True)
 +    if not instanceDefinition: ​
 +        print "{0} block does not exist"​.format(blockName)
 +        return
 +    ​
 +    newName = rs.GetString("​new name")
 +    instanceDefinition = doc.InstanceDefinitions.Find(newName,​ True)
 +    if instanceDefinition: ​
 +        print "the name '​{0}'​ is already taken by another block"​.format(newName)
 +        return
 +
 +    rs.RenameBlock(blockName,​ newName)
 +    ​
 +if __name__ == "​__main__":​
 +    Rename()
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/renameblock.txt ยท Last modified: 2015/09/14 (external edit)