Guest User

Untitled

a guest
Jun 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. import Rhino
  2. import rhinoscriptsyntax as rs
  3. import scriptcontext as sc
  4. import os
  5.  
  6. def test():
  7. max = 16 #max iterations
  8.  
  9. MainFolder = rs.WorkingFolder()
  10. if sc.sticky.has_key("MAIN_FOLDER"):
  11. MainFolder = sc.sticky["MAIN_FOLDER"]
  12.  
  13. MainFolder = rs.BrowseForFolder(MainFolder)
  14. if not MainFolder:return
  15.  
  16. sc.sticky["MAIN_FOLDER"] = MainFolder
  17.  
  18. print(MainFolder)
  19.  
  20. i = 0
  21.  
  22. # Sort the files from higher to lower arch
  23. Files = [file for file in os.listdir(MainFolder) if file.endswith('.stl')]
  24. Files = sorted(Files,reverse=True)
  25.  
  26. vecTemp = Rhino.Geometry.Vector3d(0,0,.05) #small extra vertical move after the final position has been arrived at.
  27. """
  28. Move each mesh as it comes in to the top of the previous one
  29. then back downward half or some fraction their BB depth, then half of that until intersections are found
  30. then move it back by half again and test, etc.
  31.  
  32. """
  33. for file in Files:
  34. rs.DocumentModified(False)
  35. rs.AddLayer(str(file))#add the layer with the name of the stl
  36. rs.CurrentLayer(str(file))#set the layer active
  37.  
  38. cmd= "_-Import " + chr(34) + str(MainFolder) + "\\" + str(file) + chr(34) + " _EnterEnd "#import the next file
  39. print "cmd = " + cmd
  40. rs.Command(cmd)
  41.  
  42. if rs.LastCommandResult()!= 0: continue
  43. rs.UnselectAllObjects()
  44. id = rs.LastCreatedObjects()[0]
  45. if not id: continue
  46.  
  47. if i == 0:
  48. #if this is the first time through
  49. #just get a copy of the mesh, set it as the base mesh,
  50. #set a flag (i) and then open the next file
  51. baseMesh = rs.coercemesh(id).Duplicate()
  52. i = 1
  53. continue
  54.  
  55. # get a copy of the incoming mesh into memory
  56. mesh = rs.coercemesh(id).Duplicate()
  57.  
  58. #Get the BB of the previous mesh
  59. baseBB = baseMesh.GetBoundingBox(False)
  60. baseCorners = baseBB.GetCorners()
  61.  
  62. #Get the BB of the current mesh
  63. bb = mesh.GetBoundingBox(False)
  64. corners = bb.GetCorners()
  65.  
  66. #Get a vector from the bottom of the current mesh BB to the top
  67. # of the previous mesh BB as a starting point for jiggling.
  68. tempDir = Rhino.Geometry.Vector3d(0,0,baseCorners[4].Z-corners[0].Z)
  69.  
  70. # move the mesh on top of the last mesh
  71. mesh.Transform(Rhino.Geometry.Transform.Translation(tempDir))
  72.  
  73. # get a vector representing the depth of the current mesh in z
  74. vecDir = corners[0]-corners[4]
  75. #make the vector smaller.
  76. vecDir = vecDir*.25
  77.  
  78. last = False
  79. count = 0
  80.  
  81. while True:
  82.  
  83. count += 1
  84.  
  85. # find the intersection of the two meshes
  86. x = Rhino.Geometry.Intersect.Intersection.MeshMeshFast(mesh, baseMesh)
  87.  
  88. if count > max: # hit the max iterations
  89. break
  90.  
  91. if x.Count == 0 and last == False:
  92. #print "None_False"
  93. mesh.Translate(vecDir)
  94. continue
  95.  
  96. elif x.Count == 0 and last == True:
  97. #print "None_True"
  98. last = False
  99.  
  100. #shrink the vector and reverse it
  101. vecDir = vecDir*.5
  102. vecDir.Reverse()
  103. mesh.Translate(vecDir)
  104. continue
  105.  
  106. elif x.Count != 0 and last == True:
  107. #print "Some_True"
  108. mesh.Translate(vecDir)
  109. continue
  110.  
  111. elif x.Count != 0 and last == False:
  112. #print "Some_False"
  113. last = True
  114. vecDir = vecDir*.5
  115. vecDir.Reverse()
  116. mesh.Translate(vecDir)
  117. continue
  118.  
  119. #make the last prcessed mesh the one to test against
  120. baseMesh = mesh
  121.  
  122. #add the last processed mesh to the doc
  123. sc.doc.Objects.Replace(id, baseMesh)
  124.  
  125. sc.doc.Views.Redraw()
  126.  
  127. test()
Add Comment
Please, Sign In to add comment