Hi @diff-arch ,the code is post here, the propblem of the code is under MyParticleSystem
's
Update
method, thanks
from scriptcontext import doc
import System
import Rhino
from Rhino import *
import random
import rhinoscriptsyntax as rs
import Eto.Forms
import math
BOUNDBOX = Geometry.Box(Geometry.BoundingBox(Geometry.Point3d(-5000,-5000,-5000),Geometry.Point3d(5000,5000,5000)))
class DrawMeshConduit(Rhino.Display.DisplayConduit):
def __init__(self,particleSystem):
self.particleSystem = particleSystem;
self.IsReflesh = True
self.Enabled = True
def UpdateParticle(self):
while self.IsReflesh:
try:
self.particleSystem = self.particleSystem.Update()
if len(list(self.particleSystem.GetEnumerator())) == 0:
self.Enable = False
raise Exception("Run complete")
except Exception as ex:
self.Enable = False
self.IsReflesh = False
rs.Redraw()
rs.Sleep(10)
rs.Redraw()
def CalculateBoundingBox(self,e):
min = Geometry.Point3d(-10000,-10000,-10000)
max = Geometry.Point3d(10000,10000,10000)
box = Geometry.BoundingBox(min,max)
e.IncludeBoundingBox(box)
def PreDrawObjects(self, drawEventArgs):
bm = System.Drawing.Bitmap("D:\pic.jpg");
dbm = Rhino.Display.DisplayBitmap(bm);
drawEventArgs.Display.DrawParticles(self.particleSystem,dbm)
drawEventArgs.Display.DrawBox(BOUNDBOX,System.Drawing.Color.Blue)
class MyParticleSystem(Rhino.Geometry.ParticleSystem):
def __init__(self,startPt):
self.pt = startPt
def Init(self):
self.Clear()
for i in range(0,2000):
random.seed = System.DateTime.Millisecond
live = 50000
size = 6.0
G = 255 if i%255>255 else i%255
B = 255 if (i*2)%255>255 else (i*2)%255
color = System.Drawing.Color.FromArgb(255,255,G,B)
speed = random.randint(100,500)
direction = Geometry.Vector3d(random.randint(-10,10),random.randint(-10,10),random.randint(-10,10))
direction.Unitize()
p = MyParticle(self.pt,color,speed,live,direction,size)
self.Add(p)
def Reset(self):
self.Init()
def Update(self):
#because the Remove method not useful, so i must to crate an new particlesystem to return
tempSys = MyParticleSystem(self.pt)
for p in self:
if not p.IsDie():
parent = p.ParentSystem # will get null
parent.Remove(p) # not work
p.Update()
tempSys.Add(p)
self.Clear()
return tempSys
class MyParticle(Rhino.Geometry.Particle):
def __init__(self,location,color,speed,live,direction,size):
self.Speed = speed
self.live = live
self.Color = color
self.Location = location
self.createTime = System.DateTime.Now
self.direction = direction
self.Size = size
def Update(self):
x = self.Location.X + self.direction.X*self.Speed
y = self.Location.Y + self.direction.Y*self.Speed
z = self.Location.Z + self.direction.Z*self.Speed
self.Location = Geometry.Point3d(x,y,z)
def IsDie(self):
if (System.DateTime.Now - self.createTime).TotalMilliseconds > self.live:
return True
else:
return False
def Run():
ps = MyParticleSystem(Geometry.Point3d(0,0,0))
ps.Init()
rs.Sleep(100)
conduit = DrawMeshConduit(ps)
conduit.UpdateParticle()
if __name__ == "__main__":
Run()