2016-10-19 22 views
0

特定のレイヤーのポリラインをすべて選択し、通常のautocad _JOINコマンドで結合しようとしています。何らかの理由で私はちょうどそれを働かせることを傾ける。C#でAutoCADでポリラインに結合

selectionsetが適切にそれをループ私ができるとして発見され、ポリラインの色を変更された(単にテスト目的のためにやったこと)

私はここで間違っている/何をしないのですか?

doc.SendStringToExecute("._JOIN\n_p\n\n", true, false, false); 

私はそれに100%満足していないが、まあ、それはです:

 [CommandMethod("JOINPOLY", 
         CommandFlags.UsePickSet | 
         CommandFlags.Redraw | 
         CommandFlags.Modal)] 
    public void SelectAllPolylineByLayer() 
    { 
     Document doc = Application.DocumentManager.MdiActiveDocument; 
     Database db = doc.Database; 
     Editor ed = doc.Editor; 

     using (Transaction tr = db.TransactionManager.StartTransaction()) 
     { 
      try 
      { 
       // create the typevalue (criteria what should be selected) 
       TypedValue[] tvs = new TypedValue[] { 
           new TypedValue(Convert.ToInt32(DxfCode.Operator), "<and"), 
           new TypedValue(Convert.ToInt32(DxfCode.LayerName), "Test unlocked"), 
           new TypedValue(Convert.ToInt32(DxfCode.Operator), "<or"), 
           new TypedValue(Convert.ToInt32(DxfCode.Start), "POLYLINE"), 
           new TypedValue(Convert.ToInt32(DxfCode.Start), "LWPOLYLINE"), 
           new TypedValue(Convert.ToInt32(DxfCode.Start), "POLYLINE2D"), 
           new TypedValue(Convert.ToInt32(DxfCode.Start), "POLYLINE3d"), 
           new TypedValue(Convert.ToInt32(DxfCode.Operator), "or>"), 
           new TypedValue(Convert.ToInt32(DxfCode.Operator), "and>") 
       }; 

       // create a selectionfilter out of our created typevalue 
       SelectionFilter oSf = new SelectionFilter(tvs); 
       PromptSelectionResult selRes = ed.SelectAll(oSf); 

       // if there is a problemw ith the promtselection stop here 
       if (selRes.Status != PromptStatus.OK) 
       { 
        ed.WriteMessage("\nError in getting the selectAll"); 
        return; 
       } 

       SelectionSet ss = selRes.Value; 

       ed.Command("_JOIN", ss, ""); 
       tr.Commit();          
      } 
      //Catch the error and write the errormessage 
      catch (System.Exception ex) 
      { 
       ed.WriteMessage(Convert.ToString(ex)); 
      } 
     } 
    } 

答えて

0

誰もが、まだこれが最終的な結果であったことを気にかけた場合、私は、私は実体を持つクラスのリストにすべてのエンティティを救う最後 に思い付いた始点と

を保存したポリラインの終点、私は(スタートし、リストの試合中の要素の端点場合を比較し、entity.joinとそれらを結合する)

 /// <summary> 
    /// Gets a layerName and tries to join all polylines on the given layer   
    /// sends back a little log message to display 
    /// </summary> 
    /// <param name="layerName"></param> 
    /// <returns></returns> 
    public static string JoinPolylineOnLayer(Database db, string layerName) 
    { 
     Document doc = Application.DocumentManager.MdiActiveDocument; 
     Editor ed = doc.Editor;       

     TypedValue[] tvs = null;    

     using (Transaction tr = db.TransactionManager.StartTransaction()) 
     { 
      LayerTable layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead); 

      try 
      { 
       // get layerid of the selected layer 
       var layerId = layerTable[layerName]; 

       // open layer table record with write privileges 
       // if the layer is locked return with an error message that the layer cant be deleted 
       LayerTableRecord layer = (LayerTableRecord)tr.GetObject(layerId, OpenMode.ForWrite); 
       if (layer.IsLocked) 
        return "' cannot be merged(locked)."; 

       // create the typevalue (criteria what should be selected) 
       tvs = new TypedValue[] { 
           new TypedValue(Convert.ToInt32(DxfCode.Operator), "<and"), 
           new TypedValue(Convert.ToInt32(DxfCode.LayerName), layerName), 
           new TypedValue(Convert.ToInt32(DxfCode.Operator), "<or"), 
           new TypedValue(Convert.ToInt32(DxfCode.Start), "POLYLINE"), 
           new TypedValue(Convert.ToInt32(DxfCode.Start), "LWPOLYLINE"), 
           new TypedValue(Convert.ToInt32(DxfCode.Start), "POLYLINE2D"), 
           new TypedValue(Convert.ToInt32(DxfCode.Start), "POLYLINE3D"), 
           new TypedValue(Convert.ToInt32(DxfCode.Operator), "or>"), 
           new TypedValue(Convert.ToInt32(DxfCode.Operator), "and>") 
       }; 

       // create a list of the entities 
       List<PolylineClass> entities = FillListOfEntities(tvs, tr, ed); 

       for (int i = entities.Count - 1; i >= 0; i--) 
       {       
        for (int j = i - 1; j >= 0; j--) 
        { 
         try 
         { 
          // check if start/endpoints are the same 
          // if they are join them and reset the loops and start again 
          if ((entities[i].StartPoint == entities[j].StartPoint) || 
           (entities[i].StartPoint == entities[j].EndPoint) || 
           (entities[i].EndPoint == entities[j].StartPoint) || 
           (entities[i].EndPoint == entities[j].EndPoint)) 
          { 
           Entity srcPLine = entities[i].Ent; 
           Entity addPLine = entities[j].Ent; 

           // join both entities 
           srcPLine.UpgradeOpen(); 
           srcPLine.JoinEntity(addPLine); 

           // delete the joined entity 
           addPLine.UpgradeOpen(); 
           entities.RemoveAt(j); 
           addPLine.Erase(); 

           // set new start and end point of the joined polyline 
           entities[i - 1] = new PolylineClass(srcPLine, GetStartPointData(srcPLine), GetEndPointData(srcPLine)); 

           // reset i to the start (as it has changed) 
           i = entities.Count; 
           j = 0; 
          } 
         } 
         catch (System.Exception ex) 
         { 
          ed.WriteMessage("\nError: n{0}", ex.Message); 
         } 
        } 
       } 
       tr.Commit(); 
       return "' have been joined"; 
      } 
      //Catch the error and write the errormessage 
      catch (System.Exception ex) 
      { 
       return Convert.ToString(ex); 
      } 
     } 
    } 


    /// <summary> 
    /// Function to fill the entities list with a give TypedValue 
    /// </summary> 
    /// <param name="tvs"></param> 
    /// <param name="tr"></param> 
    /// <param name="ed"></param> 
    /// <returns></returns> 
    private static List<PolylineClass> FillListOfEntities(TypedValue[] tvs, Transaction tr, Editor ed) 
    { 
     SelectionFilter oSf = new SelectionFilter(tvs); 
     PromptSelectionResult selRes = ed.SelectAll(oSf); 

     // if there is a problemw ith the promtselection stop here 
     if (selRes.Status != PromptStatus.OK) 
     { 
      return null; 
     } 

     // declare a list and fill it with all elements from our selectionfilter 
     List<PolylineClass> entities = new List<PolylineClass>(); 

     foreach (ObjectId obj in selRes.Value.GetObjectIds()) 
     { 
      Entity ent = tr.GetObject(obj, OpenMode.ForRead) as Entity; 
      entities.Add(new PolylineClass(ent, GetStartPointData(ent), GetEndPointData(ent))); 
     } 

     return entities; 
    } 


    /// <summary> 
    /// Function to get the startpoint coordinates of a polyline 
    /// </summary> 
    /// <param name="obj"></param> 
    /// <returns></returns> 
    private static Point3d GetStartPointData(Entity obj) 
    {    
     // If a "lightweight" (or optimized) polyline 
     Polyline lwp = obj as Polyline; 
     if (lwp != null) 
     { 
      return new Point3d(lwp.GetPoint2dAt(0).X, lwp.GetPoint2dAt(0).Y, lwp.Elevation); 
     } 
     else 
     { 
      // If an old-style, 2D polyline 
      Polyline2d p2d = obj as Polyline2d; 
      if (p2d != null) 
      { 
       return new Point3d (p2d.StartPoint.X, p2d.StartPoint.Y, p2d.Elevation); 
      } 
      else 
      { 
       // If an old-style, 3D polyline 
       Polyline3d p3d = obj as Polyline3d; 
       if (p3d != null) 
       { 
        return p3d.StartPoint; 
       } 
      } 
     } 
     return new Point3d(0, 0, 0); 
    } 


    /// <summary> 
    /// Function to get the endpoint coordinates of a polyline 
    /// </summary> 
    /// <param name="obj"></param> 
    /// <returns></returns> 
    private static Point3d GetEndPointData(Entity obj) 
    { 
     // If a "lightweight" (or optimized) polyline 
     Polyline lwp = obj as Polyline; 
     if (lwp != null) 
     { 
      return new Point3d(lwp.GetPoint2dAt(lwp.NumberOfVertices - 1).X, lwp.GetPoint2dAt(lwp.NumberOfVertices - 1).Y, lwp.Elevation); 
     } 
     else 
     { 
      // If an old-style, 2D polyline 
      Polyline2d p2d = obj as Polyline2d; 
      if (p2d != null) 
      { 
       return new Point3d(p2d.EndPoint.X, p2d.EndPoint.Y, p2d.Elevation); 
      } 
      else 
      { 
       // If an old-style, 3D polyline 
       Polyline3d p3d = obj as Polyline3d; 
       if (p3d != null) 
       { 
        return p3d.EndPoint; 
       } 
      } 
     } 
     return new Point3d(0, 0, 0); 
    } 
0

エディタコマンドは現在、以下の私が使用している回避策として

に参加するクライアントとは異なるように思わ私は今のところ行く必要があるもの。

0

正しい方法は(AutoCAD> = 2013のバージョンを使用している場合)Polyline.JoinEntitiesメソッドを使用することです。

ドキュメントを読むことを忘れないでください:

Polyline.JoinEntitiesは、共通の開始または終了を共有する、他、閉じていないポリラインまたはPolyline2d、ライン、および/またはアークの実体であることを指定したエンティティが必要ですポイント

3Dポリラインを処理するには、それらを線/ 2Dポリラインに変換する必要があります(もちろん、すべてのエンティティは同じ平面になければなりません)。

サンプルがここにあります:http://adndevblog.typepad.com/autocad/2012/05/joining-2d-3d-polylines.html

+0

私は薄い に見て私に良い点を与えますkは私の問題を少し誤解しました.3dポリラインと2dを結合したくありません。私はちょうど他の3Dポリスとすべての3Dポリスに参加し、他の2Dポリスとすべて2Dに参加したい。 – Smir

+0

この目的のために、専用のルーチンを購入することを検討しましたか? 2Dおよび3Dポリラインの複数の結合を行うために購入できるこのルーチンがあるウェブサイトを知っています。ホイールを再発明する必要がありません。 –

関連する問題