PointCloud AddRange

Hi Terry (@Terry_Chappell)

I assume PointCloud is not completely implemented in CPython’s rhino3dm module:

So… I’ve decided to go back to IronPython but this time I used the standalone installation (not the Rhino embedded engine)

This works using my method (csv) perhaps you can apply your method with the dll and make it even faster. Now it doesn’t rely on Rhino application that has tons of unnecessary things done in background.

"""
A script to import points and colors from txt file, then create a PointCloud
and save the 3dm file to the desktop

by Ivelin Peychev

Prerequisites:
I. DotNet libraries:
    1. IronPython2.7.x (standalone installation)
    2. Rhino3dmIO.dll
    3. librhino3dmio_native.dll (this is included in the Rhino3dmIO nuget package
    4. System namespace included in the "mscorlib.dll" should be in the system.
    5. System.Drawing for the Color
    6. System.Windows.Forms for the open file dialog
II. Python modules:
    1. csv - for reading the txt file in a friendlier manner.
    2. time - for tracking the time needed for the task to complete
    3. os - for saving the file
"""

import clr

"""
using just this one leads to error that librhino3dmio_native.dll cannot be found
librhino3dmio_native.dll has to be in the same folder as Rhino3dmIO.dll
"""

clr.AddReferenceToFileAndPath(r"Z:\ipy2\nuget_dlls\McNeel\Rhino3dmIO.Desktop\lib\net45\Rhino3dmIO.dll")
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
clr.AddReference("mscorlib")

import os
import time
import csv

import System
import Rhino
import Rhino.FileIO

from System.Windows.Forms import OpenFileDialog, DialogResult
from System.Drawing import Color

def TST():
    openFileDialog = OpenFileDialog()
    openFileDialog.InitialDirectory = "c:\\"
    openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    openFileDialog.FilterIndex = 2
    openFileDialog.RestoreDirectory = True
    if (openFileDialog.ShowDialog() == DialogResult.OK):
        #Get the path of specified file
        strPath = openFileDialog.FileName
        print strPath
        
    
    if not strPath: return
    
    """ So far so good I can get the path to the txt containing points"""
    
    # Create a File3dm object
    model = Rhino.FileIO.File3dm()
    
    # create point cloud
    cloud = Rhino.Geometry.PointCloud()
    ts = time.time()
    
    # I don't get the underscore ?
    point_list = Rhino.Collections.Point3dList()
    tcolors = System.Collections.Generic.List[Color]()
    with open(strPath,'r') as csvfile:
        
        dict_reader = csv.DictReader(csvfile, fieldnames=['x','y','z','r','g','b'], restkey=None, restval=None, dialect='excel',delimiter=' ', quotechar="'",quoting=csv.QUOTE_NONNUMERIC)
        # csv.QUOTE_NONNUMERIC -> tells the reader to convert all non quoted data to floats
        
        for row in dict_reader:
            #model.Objects.AddPoint(float(row['x']),float(row['y']),float(row['z']))
            point_list.Add(row['x'],row['y'],row['z'])
            tcolors.Add(Color.FromArgb(row['r'],row['g'],row['b']))
            #cloud.Add(Rhino.Geometry.Point3d(row['x'],row['y'],row['z']),Color.FromArgb(row['r'],row['g'],row['b']))#
            
    """rhino3dm.PointCloud does not have attribute AddRange"""
    cloud.AddRange(point_list,tcolors)
    
    
    model.Objects.AddPointCloud(cloud)
    #sc.doc.Objects.AddPointCloud(cloud)
    
    # Full path to 3dm file to save
    desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop') 
    filename = 'point_cloud.3dm'
    path = os.path.join(desktop, filename)
    
    # Write model to disk
    model.Write(path, 6)
    
    
    print ("Elapsed time is {:.2f}".format(time.time()-ts))
    print ("Done!")

if __name__ == "__main__":
    
    TST()
    


https://www.nuget.org/packages?q=rhino3dmio
https://ironpython.net/download/