Last active
June 26, 2018 21:32
-
-
Save pgolay/af928d18b12259779b127cb4af806fd9 to your computer and use it in GitHub Desktop.
Tries to stack a series of meshes from different files with minimal clearance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Rhino | |
import rhinoscriptsyntax as rs | |
import scriptcontext as sc | |
import os | |
def test(): | |
max = 16 #max iterations | |
MainFolder = rs.WorkingFolder() | |
if sc.sticky.has_key("MAIN_FOLDER"): | |
MainFolder = sc.sticky["MAIN_FOLDER"] | |
MainFolder = rs.BrowseForFolder(MainFolder) | |
if not MainFolder:return | |
sc.sticky["MAIN_FOLDER"] = MainFolder | |
print(MainFolder) | |
i = 0 | |
# Sort the files from higher to lower arch | |
Files = [file for file in os.listdir(MainFolder) if file.endswith('.stl')] | |
Files = sorted(Files,reverse=True) | |
vecTemp = Rhino.Geometry.Vector3d(0,0,.05) #small extra vertical move after the final position has been arrived at. | |
""" | |
Move each mesh as it comes in to the top of the previous one | |
then back downward half or some fraction their BB depth, then half of that until intersections are found | |
then move it back by half again and test, etc. | |
""" | |
for file in Files: | |
rs.DocumentModified(False) | |
rs.AddLayer(str(file))#add the layer with the name of the stl | |
rs.CurrentLayer(str(file))#set the layer active | |
cmd= "_-Import " + chr(34) + str(MainFolder) + "\\" + str(file) + chr(34) + " _EnterEnd "#import the next file | |
print "cmd = " + cmd | |
rs.Command(cmd) | |
if rs.LastCommandResult()!= 0: continue | |
rs.UnselectAllObjects() | |
id = rs.LastCreatedObjects()[0] | |
if not id: continue | |
if i == 0: | |
#if this is the first time through | |
#just get a copy of the mesh, set it as the base mesh, | |
#set a flag (i) and then open the next file | |
baseMesh = rs.coercemesh(id).Duplicate() | |
i = 1 | |
continue | |
# get a copy of the incoming mesh into memory | |
mesh = rs.coercemesh(id).Duplicate() | |
#Get the BB of the previous mesh | |
baseBB = baseMesh.GetBoundingBox(False) | |
baseCorners = baseBB.GetCorners() | |
#Get the BB of the current mesh | |
bb = mesh.GetBoundingBox(False) | |
corners = bb.GetCorners() | |
#Get a vector from the bottom of the current mesh BB to the top | |
# of the previous mesh BB as a starting point for jiggling. | |
tempDir = Rhino.Geometry.Vector3d(0,0,baseCorners[4].Z-corners[0].Z) | |
# move the mesh on top of the last mesh | |
mesh.Transform(Rhino.Geometry.Transform.Translation(tempDir)) | |
# get a vector representing the depth of the current mesh in z | |
vecDir = corners[0]-corners[4] | |
#make the vector smaller. | |
vecDir = vecDir*.25 | |
last = False | |
count = 0 | |
while True: | |
count += 1 | |
# find the intersection of the two meshes | |
x = Rhino.Geometry.Intersect.Intersection.MeshMeshFast(mesh, baseMesh) | |
if count > max: # hit the max iterations | |
break | |
if x.Count == 0 and last == False: | |
#print "None_False" | |
mesh.Translate(vecDir) | |
continue | |
elif x.Count == 0 and last == True: | |
#print "None_True" | |
last = False | |
#shrink the vector and reverse it | |
vecDir = vecDir*.5 | |
vecDir.Reverse() | |
mesh.Translate(vecDir) | |
continue | |
elif x.Count != 0 and last == True: | |
#print "Some_True" | |
mesh.Translate(vecDir) | |
continue | |
elif x.Count != 0 and last == False: | |
#print "Some_False" | |
last = True | |
vecDir = vecDir*.5 | |
vecDir.Reverse() | |
mesh.Translate(vecDir) | |
continue | |
#make the last prcessed mesh the one to test against | |
baseMesh = mesh | |
#add the last processed mesh to the doc | |
baseMesh.Translate(vecTemp) | |
sc.doc.Objects.Replace(id, baseMesh) | |
sc.doc.Views.Redraw() | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment