Jump to content

Dynamic block intersection with Line


Frederick

Recommended Posts

Hello guys,
I'm trying to do a intersection  dynamic block (name = "handrail") with user selected Line.

This dynamic block is made up of polylines only.

In the intersections points a need make a circles.

This is my code, but is not working with dynamic block, only with static block.
 

  public static void FindIntersection(List<ObjectId> polylineIds)
  {
      Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

      PromptEntityOptions peo2 = new PromptEntityOptions("\nSelect a Line: ");
      peo2.SetRejectMessage("\nError, please select a Line");
      peo2.AddAllowedClass(typeof(Line), true);

      PromptEntityResult res2 = ed.GetEntity(peo2);
      if (res2.Status != PromptStatus.OK)
      {
          return;
      }

      ObjectId lineId = polylineIds[0];
      ObjectId line2Id = res2.ObjectId;

      Database db = Application.DocumentManager.MdiActiveDocument.Database;

      using (Transaction trans = db.TransactionManager.StartTransaction())
      {
          Polyline poly = trans.GetObject(lineId, OpenMode.ForRead) as Polyline;
          Line line = trans.GetObject(line2Id, OpenMode.ForRead) as Line;

          BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
          BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

          Line line2 = new Line();
          line2.StartPoint = poly.StartPoint;
          line2.EndPoint = poly.EndPoint;
          line2.ColorIndex = 1;

          btr.AppendEntity(line2);
          trans.AddNewlyCreatedDBObject(line2, true);

          Point3dCollection intersectionPoints = new Point3dCollection();
          line.IntersectWith(poly, Intersect.OnBothOperands, intersectionPoints, 0, 0);

          Point3d intersectionPoint = intersectionPoints[0];

          double radius = 25.0; // Průměr 50
          Circle circle = new Circle(intersectionPoint, Vector3d.ZAxis, radius);

          btr.AppendEntity(circle);
          trans.AddNewlyCreatedDBObject(circle, true);

          trans.Commit();

   
      }

  }


  [CommandMethod("Intersections")]
  public static void CountPolylinesInBlock()
  {
      var doc = Application.DocumentManager.MdiActiveDocument;
      var db = doc.Database;
      var ed = doc.Editor;


      string blockName = "Handrail";

     
      var polylineIds = new List<ObjectId>();

      using (Transaction tr = db.TransactionManager.StartTransaction())
      {
        
          var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

          if (!bt.Has(blockName))
          {
              ed.WriteMessage($"\nBlok '{blockName}' not found.");
              return;
          }
         

       
          var blkRecId = bt[blockName];
          var blkRec = (BlockTableRecord)tr.GetObject(blkRecId, OpenMode.ForRead);


       
          foreach (ObjectId id in blkRec)
          {
              Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
              if (ent != null && ent is Polyline)
              {

                  polylineIds.Add(id);
              }
          }

    
          tr.Commit();
      }

      FindIntersection(polylineIds);
  }



image.thumb.png.68156feb5eb15b917796967fce8b2440.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...