Site Tools


Differences

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

Link to this comparison view

developer:rhinocommonsamples:deleteblock [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: Delete Instance Definition (Block) =====
 +
 +===== C# =====
 +<code c#>
 +[System.Runtime.InteropServices.Guid("​E0699E15-C801-4FC9-AE0A-17B5B9168CD0"​)]
 +public class DeleteBlockCommand : Command
 +{
 +  public override string EnglishName { get { return "​csDeleteInstanceDefinition";​ } }
 +
 +  protected override Result RunCommand(RhinoDoc doc, RunMode mode)
 +  {
 +    // Get the name of the instance definition to rename
 +    string instanceDefinitionName = "";​
 +    var rc = RhinoGet.GetString("​Name of block to delete",​ 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 can be deleted
 +    if (instanceDefinition.IsReference) {
 +      RhinoApp.WriteLine(string.Format("​Unable to delete block \"​{0}\"​.",​ instanceDefinitionName));​
 +      return Result.Nothing;​
 +    }
 +
 +    // delete block and all references
 +    if (!doc.InstanceDefinitions.Delete(instanceDefinition.Index,​ true, true)) {
 +      RhinoApp.WriteLine(string.Format("​Could not delete {0} block",​ instanceDefinition.Name));​
 +      return Result.Failure;​
 +    }
 +
 +    return Result.Success;​
 +  }
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +<​System.Runtime.InteropServices.Guid("​93453BA7-6F93-46F4-AEF8-5A90D15AD4E9"​)>​ _
 +Public Class DeleteBlockCommand
 +  Inherits Command
 +  Public Overrides ReadOnly Property EnglishName() As String
 +    Get
 +      Return "​vbDeleteInstanceDefinition"​
 +    End Get
 +  End Property
 +
 +  Protected Overrides Function RunCommand(doc As RhinoDoc, mode As RunMode) As Result
 +    ' Get the name of the instance definition to rename
 +    Dim instanceDefinitionName As String = ""​
 +    Dim rc = Rhino.Input.RhinoGet.GetString("​Name of block to delete",​ 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 can be deleted
 +    If instanceDefinition.IsReference Then
 +      RhinoApp.WriteLine([String].Format("​Unable to delete block ""​{0}""​.",​ instanceDefinitionName))
 +      Return Result.[Nothing]
 +    End If
 +
 +    ' delete block and all references
 +    If Not doc.InstanceDefinitions.Delete(instanceDefinition.Index,​ True, True) Then
 +      RhinoApp.WriteLine([String].Format("​Could not delete {0} block",​ instanceDefinition.Name))
 +      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 Delete():
 +    blockName = rs.GetString("​block to delete"​)
 +    instanceDefinition = doc.InstanceDefinitions.Find(blockName,​ True)
 +    if not instanceDefinition:​
 +        print "{0} block does not exist"​.format(blockName)
 +        return
 +        ​
 +    rs.DeleteBlock(blockName)
 +    ​
 +if __name__ == "​__main__":​
 +    Delete()
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/deleteblock.txt ยท Last modified: 2015/09/14 (external edit)