def splitModel(objs, cutLevel):
    point = Rhino.Geometry.Point3d(0,0,cutLevel)
    
    belowDir = rs.AddLine(point, [0,0,-9999])
    aboveDir = rs.AddLine(point, [0,0,9999])
    circle = rs.AddCircle(point, 9999)
    circleSrf = rs.AddPlanarSrf(circle)
    
    aboveGroup = rs.AddGroup("Above")
    belowGroup = rs.AddGroup("Below")
    
    
    for obj in objs:
        ptBtm = rs.BoundingBox(obj)[0]
        ptTop = rs.BoundingBox(obj)[6]
        if ptBtm[2]>cutLevel:
            intersecting = False
            rs.AddObjectToGroup(obj, "Above")
            #print "Object Above"
        elif ptTop[2]<cutLevel:
            intersecting = False
            rs.AddObjectToGroup(obj, "Below")
            #print "Object Below"
        else:
            intersecting = True
        
        if intersecting:
            if rs.IsBrep(obj):
                closed = False
                if rs.IsPolysurfaceClosed(obj):
                    closed = True
                try:
                    copy = rs.CopyObject(obj)
                    splitSrfs = rs.SplitBrep(obj, circleSrf, True)
                    for splitSrf in splitSrfs:
                        #print "looping"
                        if closed:
                            rs.CapPlanarHoles(splitSrf)
                        rs.MatchObjectAttributes(splitSrf, copy)
                        ptBtm = rs.BoundingBox(splitSrf)[0]
                        ptTop = rs.BoundingBox(splitSrf)[6]
                        mdPtZ = (ptBtm[2] + ptTop[2]) / 2
                        if mdPtZ>cutLevel:
                            rs.AddObjectToGroup(splitSrf, "Above")
                        else:
                            rs.AddObjectToGroup(splitSrf, "Below")
                    rs.DeleteObject(copy)
                    rs.DeleteObject(obj)
                except:
                    None
            if rs.IsBlockInstance(obj):
                contents = rs.ExplodeBlockInstance(obj)
                for content in contents:
                    objs.append(content)
    rs.DeleteObject(belowDir)
    rs.DeleteObject(aboveDir)
    rs.DeleteObject(circle)
    rs.DeleteObject(circleSrf)
def make2dByLayer(layer):
    objs = rs.ObjectsByLayer(layer)
    if objs is None:
        return
    #Make2d

    rs.SelectObjects(objs)
    rs.Command("-_make2d _D _U _Enter")
    projLines = rs.GetObjects("Press escape", preselect=True)
    finalCrvs = rs.JoinCurves(projLines)
    rs.DeleteObjects(projLines)
    rs.MatchObjectAttributes(finalCrvs, objs[0])
    return finalCrvs
def flatten(crvs):
    planPlane = rs.AddPlaneSurface([-1000,-1000,0], 3000, 3000)
    
    #projectedCrvs = rs.ProjectCurveToSurface(crvs, planPlane, [0,0,-1])
    projectedCrvs = []
    
    for crv in crvs:
        explodedCrvs = rs.ExplodeCurves(crv)
        if explodedCrvs:
            for explodedCrv in explodedCrvs:
                tempCrv = rs.ProjectCurveToSurface(explodedCrv, planPlane, [0,0,-1])
                rs.DeleteObject(explodedCrv)
                rs.MatchObjectAttributes(tempCrv, crv)
                projectedCrvs.append(tempCrv)
            rs.DeleteObject(crv)
        else:
            tempCrv = rs.ProjectCurveToSurface(crv, planPlane, [0,0,-1])
            rs.MatchObjectAttributes(tempCrv, crv)
            rs.DeleteObjects(crv)
            projectedCrvs.append(tempCrv)
        
    rs.DeleteObject(planPlane)
    
    return projectedCrvs
def cutAtPlan(level, join):
    cutPlane = rs.AddPlaneSurface([-1000,-1000,level], 3000, 3000)
    plane = rs.PlaneFromNormal([-1000,-1000,0], [0,0,1])
    planPlane = rs.AddPlaneSurface(plane, 3000, 3000)
    
    objs = rs.VisibleObjects()
    intersectCrvs = []
    tempCrv = None
    for obj in objs:
        if rs.IsBrep(obj):
            tempCrv = rs.IntersectBreps(obj, cutPlane)
        if tempCrv:
            rs.MatchObjectAttributes(tempCrv, obj)
            newCrvs = flatten(tempCrv)
    rs.DeleteObject(cutPlane)
    rs.DeleteObject(planPlane)
def contourPt(obj, pt):
    """
    creates a contour according to xy plane at specified height
    obj: single object to contour
    pt: a point to contour at
    """
    planPlane = rs.AddPlaneSurface([-10000, -10000, pt[2]], 30000, 30000)
    intersectCrvs = []
    tempCrv = None

    if rs.IsBrep(obj):
        tempCrv = rs.IntersectBreps(obj, planPlane)
    if tempCrv != None:
        objName = rs.ObjectName(obj)
        rs.ObjectName(tempCrv, objName)
        intersectCrvs.append(tempCrv)
        rs.MatchObjectAttributes(tempCrv, obj)
    rs.DeleteObject(planPlane)
    return intersectCrvs
Example #6
0
def IntersectGeos(objs, plane):
    tolerance = rs.UnitAbsoluteTolerance()
    finalGeo = []
    for obj in objs:
        intersectionCrvs = []
        brep = rs.coercebrep(obj)
        if brep is None: continue
        x = Rhino.Geometry.Intersect.Intersection.BrepPlane(
            brep, plane, tolerance)
        xCurves = x[1]
        if xCurves is None: continue
        try:
            rs.JoinCurves(xCurves)
        except:
            pass
        for curve in xCurves:
            finalCurve = sc.doc.Objects.AddCurve(curve)
            rs.SetUserText(finalCurve, 'PCPA_floorplan', 'intersect')
            intersectionCrvs.append(finalCurve)
            finalGeo.append(intersectionCrvs)

        rs.MatchObjectAttributes(intersectionCrvs, obj)

    return finalGeo
Example #7
0
def Rectify_AngleFirst_Button():
    objs = rs.GetObjects("Select polylines to rectify", rs.filter.curve, preselect = True)
    if objs is None: return

    if 'geometry-angleMultiple' in sc.sticky:
        angleDefault = sc.sticky['geometry-angleMultiple']
    else:
        angleDefault = 45

    if 'geometry-lengthMultiple' in sc.sticky:
        lengthDefault = sc.sticky['geometry-lengthMultiple']
    else:
        lengthDefault = 1

    angleMultiple = rs.GetReal("Round angle to multiples of", angleDefault)
    if angleMultiple is None: return
    sc.sticky['geometry-angleMultiple'] = angleMultiple


    lengthMultiple = rs.GetReal("Round length to multiples of", lengthDefault)
    if lengthMultiple is None: return
    sc.sticky['geometry-lengthMultiple'] = lengthMultiple

    for obj in objs:
        try:
            rs.SimplifyCurve(obj)
            newLine = Rectify_AngleFirst(obj, angleMultiple, lengthMultiple)
            rs.MatchObjectAttributes(newLine, obj)
            utils.SaveToAnalytics('Geometry-Rectify')
            result = True
        except:
            result = False
            print "Rectify failed"
        utils.SaveFunctionData('Geometry-Rectify', [angleMultiple, lengthMultiple,  str([(pt.X, pt.Y, pt.Z) for pt in rs.CurveEditPoints(obj)]), result])
        if result:
            rs.DeleteObject(obj)
def RunCommand(is_interactive):
    if sc.escape_test(False):
        print "script cancelled"  #do something

    print "Making unique..."

    #******* Get blocks *****************
    #************************************

    objectIds = rs.GetObjects("Pick some blocks", 4096, preselect=True)
    if not objectIds:
        print "No objects"
        return False

    #pause viewport redraw
    rs.EnableRedraw(False)

    #******* Sort blocks by type ********
    #************************************
    blockTypes = {}
    for id in objectIds:
        blockName = rs.BlockInstanceName(id)
        if blockName not in blockTypes:
            blockTypes[blockName] = []
        blockTypes[blockName].append(id)

    #***** Define new block and add *****
    #************************************

    #Get block names
    blockNames = rs.BlockNames()

    #gather all new objects when done
    finalObjs = []

    for blockType in blockTypes:
        for id in blockTypes[blockType]:
            #Get the block transformation matrix and name
            blockXForm = rs.BlockInstanceXform(id)
            blockName = rs.BlockInstanceName(id)

            #Insert new block in 0,0,0
            newBlock = rs.InsertBlock(blockName, [0, 0, 0])

            #Explode the block
            exObjs = rs.ExplodeBlockInstance(newBlock)

            #create new block name

            # if the string ends in digits m will be a Match object, or None otherwise.
            strippedName = re.sub(r'#[0-9]+$', '', blockName)

            #test if block name exist and add to the end number if true.
            x = 0
            tryAgain = True
            while tryAgain:
                x += 1
                newerBlockName = strippedName + "#" + str(x)
                if newerBlockName not in blockNames:
                    tryAgain = False
                    break

            #insert exObjs as new block
            rs.AddBlock(exObjs, [0, 0, 0], newerBlockName, delete_input=True)
            newerBlock = rs.InsertBlock(newerBlockName, [0, 0, 0])

            #match properties from original
            rs.MatchObjectAttributes(newerBlock, id)

            #transform new block
            rs.TransformObject(newerBlock, blockXForm)

            #append for final selection
            finalObjs.append(newerBlock)

        #add name to list of used blocknames.
        blockNames.append(newerBlockName)

    #Delete original block
    rs.DeleteObjects(objectIds)

    #Select all new objects
    rs.SelectObjects(finalObjs)

    rs.EnableRedraw(True)

    print "...aaand its done."
    #End RunCommand()

    #end sane
    return 0
Example #9
0
    hatch_scale = rs.HatchScale(hatch)
    
    # Make hatch solid so we able to explode it and get surface instead
    if hatch_pattern != "Solid":
        rs.HatchPattern(hatch, "Solid")
    dup_border_surface = []
    dup_border_surface.append(rs.ExplodeHatch(hatch)[0])
    rs.SurfaceIsocurveDensity(dup_border_surface, 100)
    selected_objects.append(dup_border_surface)
    reduced_selected_objects = reduce(operator.add, selected_objects)
    rs.SelectObjects(reduced_selected_objects)
    rs.HideObject(hatch)
    rs.Command("_Trim")
    trimmed_surface = rs.LastCreatedObjects()
    
    new_borders = []
    if trimmed_surface:
        for surface in trimmed_surface:
            new_borders.append(rs.DuplicateSurfaceBorder(surface))
            selected_objects.append(new_borders)
        # Keep trimming lines but everything else will be deleted further
        selected_objects.remove(trim_lines)
        new_hatches = rs.AddHatches(new_borders, hatch_pattern, hatch_scale, hatch_rotation)
        rs.MatchObjectAttributes(new_hatches, hatch)
        rs.ShowObject(new_hatches)
       rs.DeleteObjects(trimmed_surface)
    else:
        print("No trimmed surfaces was created.")
else:
    print("No hatches was selected.")
def rectify(pline, decPlaces):
    """
    --Uses your current cplane as guides
    pline: one pline to rectify
    decPlace: number of decimals to round to (1 = 100mm, 2 = 10mm, 3 = 1mm)
    """
    rs.EnableRedraw(False)

    #Remove colinear points
    rs.SimplifyCurve(pline)

    #orient to world
    xPt = rs.VectorAdd(rs.ViewCPlane().Origin, rs.ViewCPlane().XAxis)
    yPt = rs.VectorAdd(rs.ViewCPlane().Origin, rs.ViewCPlane().YAxis)
    origCplane = [rs.ViewCPlane().Origin, xPt, yPt]
    world = [[0, 0, 0], [1, 0, 0], [0, 1, 0]]
    rs.OrientObject(pline, origCplane, world)

    #get ctrl Pts
    ctrlPts = rs.CurvePoints(pline)

    #test if closed
    closedBool = rs.IsCurveClosed(pline)
    if closedBool:
        del ctrlPts[-1]

    #initial direction
    stPt = ctrlPts[0]
    nxtPt = ctrlPts[1]
    dX = abs(stPt[0] - nxtPt[0])
    dY = abs(stPt[1] - nxtPt[1])
    if dX > dY:
        xDir = True
    else:
        xDir = False

    #split into x and y vals
    xVals = []
    yVals = []
    xVals.append(ctrlPts[0][0])
    yVals.append(ctrlPts[0][1])
    if xDir:
        for i in range(1, len(ctrlPts)):
            if i % 2 == 1:
                xVals.append(ctrlPts[i][0])
            else:
                yVals.append(ctrlPts[i][1])
    else:
        for i in range(1, len(ctrlPts)):
            if i % 2 == 0:
                xVals.append(ctrlPts[i][0])
            else:
                yVals.append(ctrlPts[i][1])
    xVals = roundedDist(xVals, decPlaces)
    yVals = roundedDist(yVals, decPlaces)

    #Make points
    newPts = []
    for i in range(0, len(ctrlPts)):
        if xDir:
            if i % 2 == 0:
                newPts.append(
                    rs.coerce3dpoint([xVals[int(i / 2)], yVals[int(i / 2)],
                                      0]))
            else:
                newPts.append(
                    rs.coerce3dpoint(
                        [xVals[int(i / 2 + .5)], yVals[int(i / 2 - .5)], 0]))
        else:
            if i % 2 == 0:
                newPts.append(
                    rs.coerce3dpoint([xVals[int(i / 2)], yVals[int(i / 2)],
                                      0]))
            else:
                newPts.append(
                    rs.coerce3dpoint(
                        [xVals[int(i / 2 - .5)], yVals[int(i / 2 + .5)], 0]))

    #Close it
    if closedBool:
        if xDir:
            newPts[-1].X = newPts[0].X
        else:
            newPts[-1].Y = newPts[0].Y
        newPts.append(newPts[0])

    #make new Line
    newLine = rs.AddPolyline(newPts)

    #Cleanup
    objectsLay = rs.MatchObjectAttributes(newLine, pline)
    rs.ObjectColor(pline, (255, 0, 0))
    #rs.DeleteObject(pline)

    #Move back to original cplane
    rs.OrientObject(newLine, world, origCplane)

    rs.EnableRedraw(True)
    return newLine