Ejemplo n.º 1
0
void GCodeExport::doDrill(FILE *fl, bool changeToDrill, DrillReader *drillReader, double drillZSafe, bool sameDiameter, bool isProbe, int drillingSpeed, \
             int drillingLiftSpeed, int drillingSpindleSpeed, double drillDepth, double zsafe)
{
    char lineBuffer[1024];
    // drilling
    if(drillReader) {
        if(drillReader->firstDrill()) {
            toolNumber = 1;
            if(changeToDrill)
                changeTool(fl, drillReader->firstDrill()->diameter, zsafe, drillZSafe, sameDiameter, isProbe, (char*)"Drill",true);
            else if(isProbe)
                probe(fl, drillZSafe);
            sprintf(lineBuffer, "M03S%d\nG04P%d\n", drillingSpindleSpeed, drillingSpindleSpeed/5); // start spindle and wait
            fputs(lineBuffer, fl);
            Drill *nextDrill = drillReader->firstDrill();
            if(sameDiameter || !changeToDrill) {
                sprintf(lineBuffer,"(Drill %f)\n", nextDrill->diameter);
                fputs(lineBuffer, fl);
            }
            while(nextDrill) {
                GPoint *next = nextDrill->firstPoint;
                while(next) {
                    next->used = false;
                    next = next->next;
                }
                while(true) {
                    GPoint *closestPoint = 0;
                    double closestDistance = FLT_MAX;
                    next = nextDrill->firstPoint;
                    double tmp;
                    while(next) {
                        if(!next->used){
                            if( (tmp=next->distance(headPosition))<closestDistance) {
                                closestDistance = tmp;
                                closestPoint = next;
                            }
                        }
                        next = next->next;
                    }
                    if(closestPoint) {
                        drill(fl, *closestPoint, drillDepth, drillingSpeed, drillingLiftSpeed, drillZSafe);
                        closestPoint->used = true;
                    } else {
                        break; // goto from while
                    }
                }

                nextDrill = nextDrill->next;
                if(nextDrill) {
                    if(!sameDiameter) {
                        fputs("M5\n", fl);
                        changeTool(fl, nextDrill->diameter, drillZSafe, drillZSafe, sameDiameter, isProbe, (char*)"Drill", true);
                        sprintf(lineBuffer, "M03S%d\nG04P%d\n", drillingSpindleSpeed, drillingSpindleSpeed/5); // start spindle and wait
                        fputs(lineBuffer, fl);
                    } else {
                        sprintf(lineBuffer,"(Drill %f)\n", nextDrill->diameter);
                        fputs(lineBuffer, fl);
                    }
                }
            }
            fputs("M5\n", fl);
        }
    }
}
Ejemplo n.º 2
0
void GCodeExport::doShapes(FILE *fl, Mode mode, GShape *shapes, double diameter, double zsafe, double depth, double speed, double filamentMMPerHeadMM, double laserEnergy)
{
    // mark all shapes as unused
    GShape *nshape = shapes;
    while(nshape) {
        nshape->used = false;
        nshape = nshape->next;
    }

    while(true) {
        double closetShapeDistance = FLT_MAX;
        GShape *closestShape = 0;
        ShapeClosestpointPlace closestPos = SCP_P1_FROM;
        GShape *nextShape = shapes;
        while(nextShape) { // looking for the closest shape
            if(!nextShape->used) {
                double tmpdst;
                switch(nextShape->type()) {
                case GSHAPE_CIRCLE:
                {
                    GCircle *circle = (GCircle*)nextShape;
                    if(circle->spanAngle>=2*M_PI) {
                        if( (tmpdst=fabs(circle->center.distance(headPosition)-circle->radius))<closetShapeDistance ) {
                            closestShape = nextShape;
                            closetShapeDistance = tmpdst;
                            closestPos =  SCP_WHOLECIRCLE;
                            if(tmpdst == 0.0L) goto foundClosest;
                        }
                    } else {
                        if( (tmpdst=circle->fromPoint().distance(headPosition))<closetShapeDistance) {
                            closestShape = nextShape;
                            closetShapeDistance = tmpdst;
                            closestPos =  SCP_P1_FROM;
                            if(tmpdst == 0.0L) goto foundClosest;
                        }
                        if( (tmpdst=circle->toPoint().distance(headPosition))<closetShapeDistance) {
                            closestShape = nextShape;
                            closetShapeDistance = tmpdst;
                            closestPos =  SCP_P2_TO;
                            if(tmpdst == 0.0L) goto foundClosest;
                        }
                    }
                }
                    break;
                case GSHAPE_LINE:
                {
                    GLine *line = (GLine*)nextShape;
                    if( (tmpdst=line->p1.distance(headPosition))<closetShapeDistance) {
                        closestShape = nextShape;
                        closetShapeDistance = tmpdst;
                        closestPos =  SCP_P1_FROM;
                        if(tmpdst == 0.0L) goto foundClosest;
                    }
                    if( (tmpdst=line->p2.distance(headPosition))<closetShapeDistance) {
                        closestShape = nextShape;
                        closetShapeDistance = tmpdst;
                        closestPos =  SCP_P2_TO;
                        if(tmpdst == 0.0L) goto foundClosest;
                    }
                }
                    break;
                case GSHAPE_RECT:
                    assert("Rect in contur while export!");
                    break;
                }
            }
            nextShape = nextShape->next;
        }
        foundClosest:
        if(closestShape) { // put shape in gcode file
            GPoint st;
            GPoint en;
            bool isCCW = false;
            switch(closestShape->type()){
            case GSHAPE_CIRCLE:
            {
                GCircle *circle = (GCircle*)closestShape;
                if(closestPos ==  SCP_P1_FROM) {
                    st = circle->fromPoint();
                    en = circle->toPoint();
                    isCCW = true;
                } else if(closestPos ==  SCP_P2_TO) {
                    st = circle->toPoint();
                    en = circle->fromPoint();
                    isCCW = false;
                } else if(closestPos ==  SCP_WHOLECIRCLE) {
                    isCCW = false;
                    GLine testLine(circle->center, headPosition);
                    GPoint p1, p2;
                    int intersectCount = GIntersects::lineWithCircle(testLine,*circle, &p1, &p2, false);
                    if(intersectCount==0){
                        st = circle->center+GPoint(circle->radius, 0.0L);
                        en = st;
                    } else {
                        if(intersectCount==1 || p1.distance(headPosition)<p2.distance(headPosition)){
                            st = p1;
                            en = p1;
                        } else {
                            st = p2;
                            en = p2;
                        }
                    }
                }
            }
                break;
            case GSHAPE_LINE:
            {
                GLine *line = (GLine*)closestShape;
                if(closestPos ==  SCP_P1_FROM) {
                    st = line->p1;
                    en = line->p2;
                } else if(closestPos ==  SCP_P2_TO) {
                    st = line->p2;
                    en = line->p1;
                } else {
                    assert("Rect in contur while export2!");
                }
            }
                break;
            case GSHAPE_RECT:
                assert("Rect in contur while export3!");
                break;
            }

            if(st.distance(headPosition)>diameter) {
                moveHeadTo(fl, mode, st, zsafe);
            } else if (st.distance(headPosition)!=0.0L) {
                millingLineTo(fl, mode, st, depth, speed, filamentMMPerHeadMM, laserEnergy, diameter);
            }
            if(closestShape->type()==GSHAPE_LINE) {
                millingLineTo(fl, mode, en, depth, speed, filamentMMPerHeadMM, laserEnergy, diameter);
            } else if(closestShape->type()==GSHAPE_CIRCLE) {
                GCircle* circle = (GCircle*)closestShape;
                millingCircle(fl, mode, ((GCircle*)closestShape)->center,en,depth,speed,isCCW, circle->spanAngle*circle->radius, filamentMMPerHeadMM, laserEnergy*(circle->outer?1.0L:1.5L), diameter);
            }

            closestShape->used = true;
        } else
            break; // from while(true)
    }

    if(headZPosition!=zsafe) {
        char lineBuffer[256];
        // raise head
        sprintf(lineBuffer, "G00Z%f\n", zsafe); // for the same style numeric
        fputs(lineBuffer, fl);
        totalTime += timeForMovement(SPEED_ON_IDLE, zsafe-headZPosition);
        headZPosition = zsafe;
    }
}