Free Support Forum - aspose.com

It take a long time to convert a dwg file to png with high resolution

Thank you! I will continuous tracking.

@cuipz_coalinfo_net_cn

You are welcome. We will share updates with you as soon as they will be available.

@cuipz_coalinfo_net_cn

We have investigated the requirements on our end and there are some observations and suggestions for you in this regard. The trouble of initially slow processing are hatches. Here are our advices:

  • You may set low quality just for hatches (not for the text): Hatch = RasterizationQualityValue.Low and this improved the speed significantly: the time required to export just first part of the drawing to png according to his code without quality options is about 1000 sec. It is 90 sec. with Low quality only for hatches.

  • You may consider improve DWG for future drawings if he can to fit our product better. GRAVEL hatch pattern type which is used here is one of the most complex as it contains 42 separate hatch lines which we need to combine accurately to preserve the initial pattern. And this problem becomes more sharp when hatch boundary is far away from the origin point which is set up for hatches. This drawing contains few such hatches with origin point far away.

  • The last option is to move hatch origin point programmatically if it is far away. Here is idea and code example how to do it:

      foreach (ICadBaseEntity entity in cadImage.Entities)
      {
    
          // find hatches
          if (entity.TypeName == CadEntityTypeName.HATCH)
          {
              ICadHatch hatch = (ICadHatch)entity;
    
              ICadBaseEntity hatchEntity = (ICadBaseEntity)(hatch.BoundaryPaths[0].BoundaryPath[0].Objects[0]);
              Cad3DPoint firstPoint = null;
    
              // find first hacth boundary entity and get first point in it to understand where is boundary.
              // Only LINE and VERTEX are implemented for example. All other possible types are listed.
              switch (hatchEntity.TypeName)
              {
                  case CadEntityTypeName.ARC:
                      {
                          break;
                      }
    
                  case CadEntityTypeName.LINE:
                      {
                          ICadLine line = (ICadLine)hatchEntity;
                          firstPoint = line.FirstPoint;
                          break;
                      }
    
                  case CadEntityTypeName.VERTEX:
                      {
                          ICadVertexBase vertex = (ICadVertexBase)hatchEntity;
                          firstPoint = vertex.LocationPoint;
                          break;
                      }
    
                  case CadEntityTypeName.SPLINE:
                      {
                          break;
                      }
    
                  case CadEntityTypeName.LWPOLYLINE:
                      {
                          break;
                      }
    
                  case CadEntityTypeName.ELLIPSE:
                      {
                          break;
                      }
              }
    
              bool requiresShift = false;
              double distanceX = 0, distanceY = 0;
    
              // get the distance from the origin point to the first hatch pattern line
              // if it is far away - differences are calculated
              if (hatch.PatternDefinitions.Count > 0)
              {
                  if (Math.Abs(hatch.PatternDefinitions[0].LineBasePoint.X - firstPoint.X) > 20000 || Math.Abs(hatch.PatternDefinitions[0].LineBasePoint.Y - firstPoint.Y) > 20000)
                  {
                      requiresShift = true;
                      distanceX = hatch.PatternDefinitions[0].LineBasePoint.X - firstPoint.X;
                      distanceY = hatch.PatternDefinitions[0].LineBasePoint.Y - firstPoint.Y;
    
                  }
              }
    
              // all hacth pattern lines should be moved at the same distance to preserve hatch pattern view
              if (requiresShift)
              {
                  foreach (ICadHatchPatternData d in hatch.PatternDefinitions)
                  {
                      d.LineBasePoint.X -= distanceX;
                      d.LineBasePoint.Y -= distanceY;
                  }
              }
          }
      }
    

Two important notes for this example are: it will probably not working in 3D but this drawing is flat. And such moving of the pattern origin lines in hatch will not preserve exactly the same view like it is shown in Autocad - the pattern inside hatch boundary will be shifted. I am not sure whether this is important for you though.
The time for this approach without decreasing the quality at all is about 120 sec.

Thank you for your advices! I will test it。

I delete all hatch from the dwg file.and set low quality just for hatches (not for the text): Hatch = RasterizationQualityValue.Low。 then do raster, It take a long time 。
Is there any difference ? Can you provide your whole sample code and the dwg file?

Can you please share how long does it take? Here is the update for point 3 with the new public API:

foreach (CadBaseEntity entity in cadImage.Entities)
                        {
                            if (entity.TypeName == CadEntityTypeName.HATCH)
                            {
                                CadHatch hatch = (CadHatch)entity;

                                if (hatch.PatternDefinitions.Count > 0)
                                {
                                    Point2D firstPoint = new Point2D(hatch.PatternDefinitions[0].LineBasePoint.X, hatch.PatternDefinitions[0].LineBasePoint.Y);

                                    if (hatch.BoundaryPaths[0].BoundaryPath[0].GetType() == typeof(CadPolylineBoundaryPath))
                                    {
                                        CadPolylineBoundaryPath polyline = (CadPolylineBoundaryPath)(hatch.BoundaryPaths[0].BoundaryPath[0]);
                                        firstPoint = polyline.Vertices[0];
                                    }
                                    else
                                    {
                                        CadEdgeBoundaryPath edgePath = (CadEdgeBoundaryPath)(hatch.BoundaryPaths[0].BoundaryPath[0]);

                                        if (edgePath.Objects[0].GetType() == typeof(CadBoundaryPathLine))
                                        {
                                            CadBoundaryPathLine line = (CadBoundaryPathLine)edgePath.Objects[0];
                                            firstPoint = line.FirstPoint;
                                        }

                                        if (edgePath.Objects[0].GetType() == typeof(CadBoundaryPathCircularArc))
                                        {
                                            // TODO
                                        }

                                        if (edgePath.Objects[0].GetType() == typeof(CadBoundaryPathCircularEllipse))
                                        {
                                            // TODO
                                        }

                                        if (edgePath.Objects[0].GetType() == typeof(CadBoundaryPathSpline))
                                        {
                                            // TODO
                                        }
                                    }


                                    bool requiresShift = false;
                                    double distanceX = 0, distanceY = 0;

                                    if (Math.Abs(hatch.PatternDefinitions[0].LineBasePoint.X - firstPoint.X) > 20000
                                        || Math.Abs(hatch.PatternDefinitions[0].LineBasePoint.Y - firstPoint.Y) > 20000)
                                    {
                                        requiresShift = true;
                                        distanceX = hatch.PatternDefinitions[0].LineBasePoint.X - firstPoint.X;
                                        distanceY = hatch.PatternDefinitions[0].LineBasePoint.Y - firstPoint.Y;
                                    }

                                    if (requiresShift)
                                    {
                                        foreach (CadHatchPatternData d in hatch.PatternDefinitions)
                                        {
                                            d.LineBasePoint.X -= distanceX;
                                            d.LineBasePoint.Y -= distanceY;
                                        }
                                    }
                                }
                            }
}

Do you use the .NET SDK with the version 21.3?

@cuipz_coalinfo_net_cn

You only need to have .NET Framework installed.