Logo Search packages:      
Sourcecode: qcad version File versions  Download package

bool DL_Dxf::handleHatchData ( DL_CreationInterface creationInterface  ) 

Handles additional hatch data.

Definition at line 1255 of file dl_dxf.cpp.

References DL_HatchEdgeData::angle1, DL_HatchEdgeData::angle2, DL_HatchEdgeData::ccw, DL_HatchEdgeData::cx, DL_HatchEdgeData::cy, DL_HatchEdgeData::defined, DL_HatchEdgeData::radius, toInt(), toReal(), DL_HatchEdgeData::type, DL_HatchEdgeData::x1, DL_HatchEdgeData::x2, DL_HatchEdgeData::y1, and DL_HatchEdgeData::y2.

Referenced by processDXFGroup().

                                                                        {

    static int firstPolylineStatus = 0;

    // Allocate hatch loops (group code 91):
    if (groupCode==91 && toInt(groupValue)>0) {

        //std::cout << "allocating " << toInt(groupValue) << " loops\n";

        if (hatchLoops!=NULL) {
            delete[] hatchLoops;
            hatchLoops = NULL;
        }
        if (maxHatchEdges!=NULL) {
            delete[] maxHatchEdges;
            maxHatchEdges = NULL;
        }
        if (hatchEdgeIndex!=NULL) {
            delete[] hatchEdgeIndex;
            hatchEdgeIndex = NULL;
        }
        if (hatchEdges!=NULL) {
            for (int i=0; i<maxHatchLoops; ++i) {
                delete[] hatchEdges[i];
            }
            delete[] hatchEdges;
            hatchEdges = NULL;
        }
        maxHatchLoops = toInt(groupValue);

        //std::cout << "maxHatchLoops: " << maxHatchLoops << "\n";

        if (maxHatchLoops>0) {
            hatchLoops = new DL_HatchLoopData[maxHatchLoops];
            maxHatchEdges = new int[maxHatchLoops];
            hatchEdgeIndex = new int[maxHatchLoops];
            hatchEdges = new DL_HatchEdgeData*[maxHatchLoops];
            //std::cout << "new hatchEdges[" << maxHatchLoops << "]\n";
            for (int i=0; i<maxHatchLoops; ++i) {
                hatchEdges[i] = NULL;
                //std::cout << "hatchEdges[" << i << "] = NULL\n";
                maxHatchEdges[i] = 0;
            }
            hatchLoopIndex = -1;
            dropEdges = false;
        }
        //std::cout << "done\n";
        return true;
    }

    // Allocate hatch edges, group code 93
    if (groupCode==93 && toInt(groupValue)>0) {
        if (hatchLoopIndex<maxHatchLoops-1 && hatchLoops!=NULL &&
                maxHatchEdges!=NULL && hatchEdgeIndex!=NULL &&
                hatchEdges!=NULL) {

            //std::cout << "  allocating " << toInt(groupValue) << " edges\n";
            dropEdges = false;

            hatchLoopIndex++;
            hatchLoops[hatchLoopIndex]
            = DL_HatchLoopData(toInt(groupValue));

            maxHatchEdges[hatchLoopIndex] = toInt(groupValue);
            hatchEdgeIndex[hatchLoopIndex] = -1;
            hatchEdges[hatchLoopIndex]
            = new DL_HatchEdgeData[toInt(groupValue)];

            //std::cout << "hatchEdges[" << hatchLoopIndex << "] = new "
            //  << toInt(groupValue) << "\n";
            firstPolylineStatus = 0;
        } else {
            //std::cout << "dropping " << toInt(groupValue) << " edges\n";
            dropEdges = true;
        }
        //std::cout << "done\n";
        return true;
    }

    // Init hatch edge for non-polyline boundary (group code 72)
    if (hatchEdges!=NULL &&
            hatchEdgeIndex!=NULL &&
            maxHatchEdges!=NULL &&
            hatchLoopIndex>=0 &&
            hatchLoopIndex<maxHatchLoops &&
            hatchEdgeIndex[hatchLoopIndex] <
            maxHatchEdges[hatchLoopIndex] &&
            (atoi(values[92])&2)==0 &&   // not a polyline
            groupCode==72 &&
            !dropEdges) {

        //std::cout << "Init hatch edge for non-polyline boundary\n";
        //std::cout << "hatchLoopIndex: " << hatchLoopIndex << "\n";
        //std::cout << "maxHatchLoops: " << maxHatchLoops << "\n";

        hatchEdgeIndex[hatchLoopIndex]++;

        //std::cout << "  init edge: type: "
        //<< toInt(groupValue)
        //<< " index: " << hatchEdgeIndex[hatchLoopIndex] << "\n";

        hatchEdges[hatchLoopIndex][hatchEdgeIndex[hatchLoopIndex]]
        .type = toInt(groupValue);
        hatchEdges[hatchLoopIndex][hatchEdgeIndex[hatchLoopIndex]]
        .defined = false;

        //std::cout << "done\n";
        return true;
    }

    // Handle hatch edges for non-polyline boundaries
    //   (group codes 10, 20, 11, 21, 40, 50, 51, 73)
    if (!dropEdges &&
            hatchEdges!=NULL &&
            hatchEdgeIndex!=NULL &&
            hatchLoopIndex>=0 &&
            hatchLoopIndex<maxHatchLoops &&
            hatchEdges[hatchLoopIndex]!=NULL &&
            hatchEdgeIndex[hatchLoopIndex]>=0 &&
            hatchEdgeIndex[hatchLoopIndex] <
            maxHatchEdges[hatchLoopIndex] &&
            ((atoi(values[92])&2)==0) &&        // not a polyline
            (groupCode==10 || groupCode==20 ||
             groupCode==11 || groupCode==21 ||
             groupCode==40 || groupCode==50 ||
             groupCode==51 || groupCode==73)) {

        //std::cout << "Handle hatch edge for non-polyline boundary\n";
        //std::cout << "  found edge data: " << groupCode << "\n";
        //std::cout << "     value: " << toReal(groupValue) << "\n";

        // can crash:
        //std::cout << "     defined: "
        //   << (int)hatchEdges[hatchLoopIndex]
        //   [hatchEdgeIndex[hatchLoopIndex]].defined << "\n";

        //std::cout << "92 flag: '" << values[92] << "'\n";
        //std::cout << "92 flag (int): '" << atoi(values[92]) << "'\n";

        if (hatchEdges[hatchLoopIndex]
                [hatchEdgeIndex[hatchLoopIndex]].defined==false) {
            if (hatchEdges[hatchLoopIndex]
                    [hatchEdgeIndex[hatchLoopIndex]].type==1) {
                switch (groupCode) {
                case 10:
                    hatchEdges[hatchLoopIndex]
                    [hatchEdgeIndex[hatchLoopIndex]].x1
                    = toReal(groupValue);
                    break;
                case 20:
                    hatchEdges[hatchLoopIndex]
                    [hatchEdgeIndex[hatchLoopIndex]].y1
                    = toReal(groupValue);
                    break;
                case 11:
                    hatchEdges[hatchLoopIndex]
                    [hatchEdgeIndex[hatchLoopIndex]].x2
                    = toReal(groupValue);
                    break;
                case 21:
                    hatchEdges[hatchLoopIndex]
                    [hatchEdgeIndex[hatchLoopIndex]].y2
                    = toReal(groupValue);
                    hatchEdges[hatchLoopIndex]
                    [hatchEdgeIndex[hatchLoopIndex]].defined = true;
                    break;
                default:
                    break;
                }
            }

            if (hatchEdges[hatchLoopIndex]
                    [hatchEdgeIndex[hatchLoopIndex]].type==2) {
                switch (groupCode) {
                case 10:
                    hatchEdges[hatchLoopIndex]
                    [hatchEdgeIndex[hatchLoopIndex]].cx
                    = toReal(groupValue);
                    break;
                case 20:
                    hatchEdges[hatchLoopIndex]
                    [hatchEdgeIndex[hatchLoopIndex]].cy
                    = toReal(groupValue);
                    break;
                case 40:
                    hatchEdges[hatchLoopIndex]
                    [hatchEdgeIndex[hatchLoopIndex]].radius
                    = toReal(groupValue);
                    break;
                case 50:
                    hatchEdges[hatchLoopIndex]
                    [hatchEdgeIndex[hatchLoopIndex]].angle1
                    = toReal(groupValue)/360.0*2*M_PI;
                    break;
                case 51:
                    hatchEdges[hatchLoopIndex]
                    [hatchEdgeIndex[hatchLoopIndex]].angle2
                    = toReal(groupValue)/360.0*2*M_PI;
                    break;
                case 73:
                    hatchEdges[hatchLoopIndex]
                    [hatchEdgeIndex[hatchLoopIndex]].ccw
                    = (bool)toInt(groupValue);
                    hatchEdges[hatchLoopIndex]
                    [hatchEdgeIndex[hatchLoopIndex]].defined = true;
                    break;
                default:
                    break;
                }
            }
        }
        return true;
    }

    /*
    // 2003/12/31: polyline hatches can be extremely slow and are rarely used
    //
       // Handle hatch edges for polyline boundaries
       //  (group codes 10, 20, 42)
       if (!dropEdges &&
               hatchEdges!=NULL &&
               hatchEdgeIndex!=NULL &&
               hatchLoopIndex>=0 &&
               hatchLoopIndex<maxHatchLoops &&
               hatchEdges[hatchLoopIndex]!=NULL &&
               //hatchEdgeIndex[hatchLoopIndex]>=0 &&
               hatchEdgeIndex[hatchLoopIndex] <
               maxHatchEdges[hatchLoopIndex] &&
               ((atoi(values[92])&2)==2)) {        // a polyline

           if (groupCode==10 || groupCode==20 ||
                   groupCode==42) {

               std::cout << "  found polyline edge data: " << groupCode << "\n";
               std::cout << "     value: " << toReal(groupValue) << "\n";

               static double lastX = 0.0;
               static double lastY = 0.0;
               static double lastB = 0.0;

               if (firstPolylineStatus<2) {
                   switch (groupCode) {
                   case 10:
                       firstPolylineStatus++;
                       if (firstPolylineStatus==1) {
                           lastX = toReal(groupValue);
                           std::cout << "     firstX: " << lastX << "\n";
                       }
                       break;

                   case 20:
                       lastY = toReal(groupValue);
                       std::cout << "     firstY: " << lastY << "\n";
                       break;

                   case 42:
                       lastB = toReal(groupValue);
                       break;

                   default:
                       break;
                   }

                   if (firstPolylineStatus!=2) {
                       return true;
                   }
               }


               switch (groupCode) {
               case 10:
                   hatchEdgeIndex[hatchLoopIndex]++;
                   hatchEdges[hatchLoopIndex]
                   [hatchEdgeIndex[hatchLoopIndex]].type = 1;
                   hatchEdges[hatchLoopIndex]
                   [hatchEdgeIndex[hatchLoopIndex]].x1
                   = lastX;
                   hatchEdges[hatchLoopIndex]
                   [hatchEdgeIndex[hatchLoopIndex]].x2
                   = lastX = toReal(groupValue);
                   std::cout << "     X: " << lastX << "\n";
                   break;
               case 20:
                   hatchEdges[hatchLoopIndex]
                   [hatchEdgeIndex[hatchLoopIndex]].y1
                   = lastY;
                   hatchEdges[hatchLoopIndex]
                   [hatchEdgeIndex[hatchLoopIndex]].y2
                   = lastY = toReal(groupValue);
                   std::cout << "     Y: " << lastY << "\n";
                   break;
                   / *
                               case 42: {
                        // convert to arc:
                        double x1 = hatchEdges[hatchLoopIndex]
                              [hatchEdgeIndex[hatchLoopIndex]].x1;
                        double y1 = hatchEdges[hatchLoopIndex]
                              [hatchEdgeIndex[hatchLoopIndex]].y1;
                        double x2 = hatchEdges[hatchLoopIndex]
                              [hatchEdgeIndex[hatchLoopIndex]].x2;
                        double y2 = hatchEdges[hatchLoopIndex]
                              [hatchEdgeIndex[hatchLoopIndex]].y2;

                        double bulge = toReal(groupValue);

                        bool reversed = (bulge<0.0);
                        double alpha = atan(bulge)*4.0;
                        double radius;
                             double cx;
                             double cy;
                        double a1;
                        double a2;
                             double mx = (x2+x1)/2.0;
                             double my = (y2+y1)/2.0;
                        double dist = sqrt(pow(x2-x1,2) + pow(y2-y1,2)) / 2.0;

                        // alpha can't be 0.0 at this point
                             radius = fabs(dist / sin(alpha/2.0));

                             double wu = fabs(pow(radius, 2.0) - pow(dist, 2.0));
                             double h = sqrt(wu);
                        double angle = acos((x2-x1) / dist);

                             if (bulge>0.0) {
                                      angle+=M_PI/2.0;
                             } else {
                                 angle-=M_PI/2.0;
                             }

                             if (fabs(alpha)>M_PI) {
                                 h*=-1.0;
                             }

                        cx = mx + cos(angle) * h;
                        cy = my + sin(angle) * h;

                        a1 = hatchEdges[hatchLoopIndex]
                                    [hatchEdgeIndex[hatchLoopIndex]].type = 2;
                                   hatchEdges[hatchLoopIndex]
                                    [hatchEdgeIndex[hatchLoopIndex]].ccw = (toReal(groupValue)>0.0);
                                   hatchEdges[hatchLoopIndex]
                                    [hatchEdgeIndex[hatchLoopIndex]].cx = cx;
                                   hatchEdges[hatchLoopIndex]
                                    [hatchEdgeIndex[hatchLoopIndex]].cy = cy;
                                   hatchEdges[hatchLoopIndex]
                                    [hatchEdgeIndex[hatchLoopIndex]].radius = radius;
                                   } break;
                        * /

               default:
                   break;
               }
           } else {
               // end polyline boundary
               dropEdges = true;
           }

           return true;
       }
    */

    return false;
}


Generated by  Doxygen 1.6.0   Back to index