Package DrawingTools

Examples of DrawingTools.DrawingTool


        OutputStreamWriter out=null;

        try {
          out = new OutputStreamWriter(new FileOutputStream(destinationFile),"UTF-8");
          MachineConfiguration mc = MachineConfiguration.getSingleton();
          DrawingTool tool = mc.GetCurrentTool();
          out.write(mc.GetConfigLine()+";\n");
          out.write(mc.GetBobbinLine()+";\n");
          out.write("G00 G90;\n");
          tool.WriteChangeTo(out);
          tool.WriteOff(out);
         
          parser.parse(srcFile, DXFParser.DEFAULT_ENCODING);
          DXFDocument doc = parser.getDocument();
          Bounds b = doc.getBounds();
          double width = b.getMaximumX() - b.getMinimumX();
          double height = b.getMaximumY() - b.getMinimumY();
          double cx = ( b.getMaximumX() + b.getMinimumX() ) / 2.0f;
          double cy = ( b.getMaximumY() + b.getMinimumY() ) / 2.0f;
          double sy = mc.GetPaperHeight()*10/height;
          double sx = mc.GetPaperWidth()*10/width;
          double scale = (sx<sy? sx:sy ) * mc.paper_margin;
          sx = scale * (MachineConfiguration.getSingleton().reverseForGlass? -1 : 1);
          sy = scale;
         
          // count all entities in all layers
          Iterator<DXFLayer> layer_iter = (Iterator<DXFLayer>)doc.getDXFLayerIterator();
          int entity_total=0;
          int entity_count=0;
          while(layer_iter.hasNext()) {
            DXFLayer layer = (DXFLayer)layer_iter.next();
            Log("<font color='yellow'>Found layer "+layer.getName()+"</font>\n");
            Iterator<String> entity_iter = (Iterator<String>)layer.getDXFEntityTypeIterator();
            while(entity_iter.hasNext()) {
              String entity_type = (String)entity_iter.next();
              List<DXFEntity> entity_list = (List<DXFEntity>)layer.getDXFEntities(entity_type);
              Log("<font color='yellow'>+ Found "+entity_list.size()+" of type "+entity_type+"</font>\n");
              entity_total+=entity_list.size();
            }
          }
          // set the progress meter
          pm.setMinimum(0);
          pm.setMaximum(entity_total);
             
          // convert each entity
          layer_iter = doc.getDXFLayerIterator();
          while(layer_iter.hasNext()) {
            DXFLayer layer = (DXFLayer)layer_iter.next();

            Iterator<String> entity_type_iter = (Iterator<String>)layer.getDXFEntityTypeIterator();
            while(entity_type_iter.hasNext()) {
              String entity_type = (String)entity_type_iter.next();
              List<DXFEntity> entity_list = layer.getDXFEntities(entity_type);
             
              if(entity_type.equals(DXFConstants.ENTITY_TYPE_LINE)) {
                for(int i=0;i<entity_list.size();++i) {
                  pm.setProgress(entity_count++);
                  DXFLine entity = (DXFLine)entity_list.get(i);
                  Point start = entity.getStartPoint();
                  Point end = entity.getEndPoint();

                  double x=(start.getX()-cx)*sx;
                  double y=(start.getY()-cy)*sy;
                  double x2=(end.getX()-cx)*sx;
                  double y2=(end.getY()-cy)*sy;
                 
                  // is it worth drawing this line?
                  double dx = x2-x;
                  double dy = y2-y;
                  if(dx*dx+dy*dy < tool.GetDiameter()/2.0) {
                    continue;
                  }
                 
                  dx = dxf_x2 - x;
                  dy = dxf_y2 - y;

                  if(dx*dx+dy*dy > tool.GetDiameter()/2.0) {
                    if(tool.DrawIsOn()) {
                      tool.WriteOff(out);
                    }
                    tool.WriteMoveTo(out, (float)x,(float)y);
                  }
                  if(tool.DrawIsOff()) {
                    tool.WriteOn(out);
                  }
                  tool.WriteMoveTo(out, (float)x2,(float)y2);
                  dxf_x2=x2;
                  dxf_y2=y2;
                }
              } else if(entity_type.equals(DXFConstants.ENTITY_TYPE_SPLINE)) {
                for(int i=0;i<entity_list.size();++i) {
                  pm.setProgress(entity_count++);
                  DXFSpline entity = (DXFSpline)entity_list.get(i);
                  DXFPolyline polyLine = DXFSplineConverter.toDXFPolyline(entity,30);
                  boolean first=true;
                  for(int j=0;j<polyLine.getVertexCount();++j) {
                    DXFVertex v = polyLine.getVertex(j);
                    double x = (v.getX()-cx)*sx;
                    double y = (v.getY()-cy)*sy;
                    double dx = dxf_x2 - x;
                    double dy = dxf_y2 - y;
                   
                    if(first==true) {
                      first=false;
                      if(dx*dx+dy*dy > tool.GetDiameter()/2.0) {
                        // line does not start at last tool location, lift and move.
                        if(tool.DrawIsOn()) {
                          tool.WriteOff(out);
                        }
                        tool.WriteMoveTo(out, (float)x,(float)y);
                      }
                      // else line starts right here, do nothing.
                    } else {
                      // not the first point, draw.
                      if(tool.DrawIsOff()) tool.WriteOn(out);
                      if(j<polyLine.getVertexCount()-1 && dx*dx+dy*dy<tool.GetDiameter()/2.0) continue// less than 1mm movement?  Skip it.
                      tool.WriteMoveTo(out, (float)x,(float)y);
                    }
                    dxf_x2=x;
                    dxf_y2=y;
                  }
                }
              } else if(entity_type.equals(DXFConstants.ENTITY_TYPE_POLYLINE)) {
                for(int i=0;i<entity_list.size();++i) {
                  pm.setProgress(entity_count++);
                  DXFPolyline entity = (DXFPolyline)entity_list.get(i);
                  boolean first=true;
                  for(int j=0;j<entity.getVertexCount();++j) {
                    DXFVertex v = entity.getVertex(j);
                    double x = (v.getX()-cx)*sx;
                    double y = (v.getY()-cy)*sy;
                    double dx = dxf_x2 - x;
                    double dy = dxf_y2 - y;
                   
                    if(first==true) {
                      first=false;
                      if(dx*dx+dy*dy > tool.GetDiameter()/2.0) {
                        // line does not start at last tool location, lift and move.
                        if(tool.DrawIsOn()) {
                          tool.WriteOff(out);
                        }
                        tool.WriteMoveTo(out, (float)x,(float)y);
                      }
                      // else line starts right here, do nothing.
                    } else {
                      // not the first point, draw.
                      if(tool.DrawIsOff()) tool.WriteOn(out);
                      if(j<entity.getVertexCount()-1 && dx*dx+dy*dy<tool.GetDiameter()/2.0) continue// less than 1mm movement?  Skip it.
                      tool.WriteMoveTo(out, (float)x,(float)y);
                    }
                    dxf_x2=x;
                    dxf_y2=y;
                  }
                }
              }
            }
          }

          // entities finished.  Close up file.
          tool.WriteOff(out);
          tool.WriteMoveTo(out, 0, 0);
         
          ok=true;
        } catch(IOException e) {
          e.printStackTrace();
        } catch (org.kabeja.parser.ParseException e) {
View Full Code Here


  public void paintComponent(Graphics g) {
    super.paintComponent(g);    // paint background
    Graphics2D g2d = (Graphics2D)g;

    MachineConfiguration mc = MachineConfiguration.getSingleton();
    DrawingTool tool = mc.GetTool(0);
   
    paintBackground();
    paintCamera(g2d);
    paintLimits(g2d,mc);
    paintCenter(g2d);
    // TODO draw left motor
    // TODO draw right motor
    // TODO draw control box
   
    final int look_ahead=500;

    g2d.setColor(Color.BLACK);
   
    // draw image   
    if(fast_nodes.size()>0) {
      // draw the nodes
      for(int i=0;i<fast_nodes.size();++i) {
        DrawPanelNode n=fast_nodes.get(i);

        if(running) {
          if(n.line_number<=linesProcessed) {
            g2d.setColor(Color.RED);
          } else if(n.line_number<=linesProcessed+look_ahead) {
            g2d.setColor(Color.GREEN);
          } else if(prefs.getBoolean("Draw all while running", true) == false) {
            break;
          }
        }
       
        switch(n.type) {
        case TOOL:
          tool = MachineConfiguration.getSingleton().GetTool(n.tool_id);
          g2d.setStroke(tool.getStroke());
          break;
        case COLOR:
          if(!running || n.line_number>linesProcessed+look_ahead) {
            g2d.setColor(n.c);
          }
          break;
        default:
          tool.DrawLine(g2d, n.x1, n.y1, n.x2, n.y2);
          break;
        }
      }
    }
    g2d.dispose();
View Full Code Here

  private void OptimizeNodes() {
    if(instructions == null) return;
   
    MachineConfiguration mc = MachineConfiguration.getSingleton();
    DrawingTool tool = mc.GetTool(0);
   
    drawScale=0.1f;
   
    float px=0,py=0,pz=90;
    float x,y,z,ai,aj;
    int i,j;
    boolean absMode=true;
    String tool_change="M06 T";
    Color tool_color=Color.BLACK;
   
    pz=0.5f;
   
    for(i=0;i<instructions.size();++i) {
      String line=instructions.get(i);
      String[] pieces=line.split(";");
      if(pieces.length==0) continue;

      if(line.startsWith(tool_change)) {
        String numberOnly= line.substring(tool_change.length()).replaceAll("[^0-9]", "");
        int id = (int)Integer.valueOf(numberOnly, 10);
        addNodeTool(i,id);
        switch(id) {
        case 1: tool_color = Color.RED; break;
        case 2: tool_color = Color.GREEN; break;
        case 3: tool_color = Color.BLUE; break;
        default: tool_color = Color.BLACK; break;
        }
        continue;
      }
     
      String[] tokens = pieces[0].split("\\s");
      if(tokens.length==0) continue;
     
      // have we changed scale?
      // what are our coordinates?
      x=px;
      y=py;
      z=pz;
      ai=px;
      aj=py;
      for(j=0;j<tokens.length;++j) {
        if(tokens[j].equals("G20")) drawScale=2.54f; // in->cm
        else if(tokens[j].equals("G21")) drawScale=0.10f; // mm->cm
        else if(tokens[j].equals("G90")) {
          absMode=true;
          break;
        } else if(tokens[j].equals("G91")) {
          absMode=false;
          break;
        }
        else if(tokens[j].equals("G54")) break;
        else if(tokens[j].startsWith("X")) {
          float tx = Float.valueOf(tokens[j].substring(1)) * drawScale;
          x = absMode ? tx : x + tx;
        }
        else if(tokens[j].startsWith("Y")) {
          float ty = Float.valueOf(tokens[j].substring(1)) * drawScale;
          y = absMode ? ty : y + ty;
        }
        else if(tokens[j].startsWith("Z")) {
          float tz = z = Float.valueOf(tokens[j].substring(1));// * drawScale;
          z =  absMode ? tz : z + tz;
        }
        if(tokens[j].startsWith("I")) ai = Float.valueOf(tokens[j].substring(1)) * drawScale;
        if(tokens[j].startsWith("J")) aj = Float.valueOf(tokens[j].substring(1)) * drawScale;
      }
      if(j<tokens.length) continue;
      //*
      // is pen up or down?
      tool.DrawZ(z);
      if(tool.DrawIsOff()) {
        if(show_pen_up==false) {
          px=x;
          py=y;
          pz=z;
          continue;
        }
        addNodeColor(i, Color.BLUE );
      } else if(tool.DrawIsOn()) {
        addNodeColor(i, tool_color )// TODO use actual pen color
      } else {
        addNodeColor(i, Color.ORANGE );
      }
     
View Full Code Here

TOP

Related Classes of DrawingTools.DrawingTool

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.