Site Tools


Differences

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

Link to this comparison view

developer:scriptsamples:replacepointswithblocks [2015/09/14] (current)
Line 1: Line 1:
 +====== Replacing Points with Blocks ======
 +====== RhinoScript ======
 +> **Summary:​** //​Demonstrates how to replace point objects with block objects using [[developer:​rhinoscript|RhinoScript]].//​
 +
 +=====Question=====
 +I have a number of point objects in my model. I would like to replace them with a block so they appear as markers. How can I do this without running the Insert command a bunch of times?
 +
 +=====Answer=====
 +The following sample code demonstrates how to replace point objects with block objects using [[developer:​rhinoscript|RhinoScript]]. To use this script:
 +
 +  - Save the attached script to a text file with a .RVB extension.
 +  - Load the file into [[developer:​rhinoscript|RhinoScript]] using the **LoadScript** command.
 +  - Run the script over and over using the **RunScript** command.
 +
 +See the introduction section of the [[developer:​rhinoscript|RhinoScript]] help file for more details on loading and running scripts.
 +
 +
 +<code vb>
 + '​ Replaces points with blocks
 + Sub ReplacePointsWithBlocks
 +
 +   '​ Select points to replace with a block
 +   Dim arrObjects
 +   ​arrObjects = Rhino.GetObjects("​Select points to replace with a block",​ 1, True, True)
 +   If Not IsArray(arrObjects) Then Exit Sub
 +
 +   '​ Get the names of all block definitions in the document ​   ​
 +   Dim arrBlocks
 +   ​arrBlocks = Rhino.BlockNames(True)
 +   If Not IsArray(arrBlocks) Then
 +     ​Rhino.Print "No block definitions found in the document."​
 +     Exit Sub
 +   End If
 +
 +   '​ Select a block name from a list
 +   Dim strBlock
 +   ​strBlock = Rhino.ListBox(arrBlocks,​ "​Select block",​ "​Replace Points"​)
 +   If IsNull(strBlock) Then Exit Sub
 +
 +   '​ Turn off redrawing (faster)
 +   ​Rhino.EnableRedraw True      ​
 +
 +   '​ Process each selected point object
 +   Dim strObject, arrPoint
 +   For Each strObject In arrObjects
 +     '​ Get the point object'​s coordinates
 +     ​arrPoint = Rhino.PointCoordinates(strObject)
 +     '​ Insert the block at that location
 +     ​Rhino.InsertBlock strBlock, arrPoint
 +   Next
 +
 +   '​ Delete all of the point objects
 +   ​Rhino.DeleteObjects arrObjects ​  
 +
 +   '​ Turn redrawing back on     
 +   ​Rhino.EnableRedraw True      ​
 +
 + End Sub
 +
 +</​code>​
 +Also, the following script will do just the opposite - it will replace block objects with point objects.
 +
 +
 +<code vb>
 + '​ Replaces blocks with points
 + Sub ReplaceBlocksWithPoints
 +
 +   '​ Select blocks to replace with points
 +   Dim arrObjects
 +   ​arrObjects = Rhino.GetObjects("​Select blocks to replace with points",​ 4096, True, True)
 +   If Not IsArray(arrObjects) Then Exit Sub
 +
 +   '​ Turn off redrawing (faster)
 +   ​Rhino.EnableRedraw True      ​
 +
 +   '​ Process each selected block object
 +   Dim strObject, arrPoint
 +   For Each strObject In arrObjects
 +     '​ Get the block'​s insertion point
 +     ​arrPoint = Rhino.BlockInstanceInsertPoint(strObject)
 +     '​ Add a point object at that location
 +     ​Rhino.AddPoint arrPoint
 +   Next
 +
 +   '​ Delete all of the block objects
 +   ​Rhino.DeleteObjects arrObjects ​  
 +
 +   '​ Turn redrawing back on     
 +   ​Rhino.EnableRedraw True      ​
 +
 + End Sub
 +
 +</​code>​
 +
 +
 +{{tag>​Developer RhinoScript}}
  
developer/scriptsamples/replacepointswithblocks.txt ยท Last modified: 2015/09/14 (external edit)