2016-08-08 17 views
1

Das ist mein Programm, in der i-Attribut im dwg file drucken kann, aber das Problem ist, dass mtext Druck des Wert wie: Projekt: 534Phase: 1ZONE: A (0530) Projekt: 534Phase : 1Zone: A (0520) und so weiter, aber ich möchte Ausgabe wie Projekt: 534 Phase: 1 Zone: A (0530) (0520).Erstellen Sie zwei verschiedene mtext in einem einzigen Programm

[CommandMethod("ATT")] 
public void ListAttributes() 
{ 
    Document acDoc = Application.DocumentManager.MdiActiveDocument; 
    Editor ed = acDoc.Editor; 
    Database db = acDoc.Database; 
    using (Transaction tr = db.TransactionManager.StartTransaction()) 
    { 
     // Start the transaction 
     try 
     { 
      // Build a filter list so that only 
      // block references with attributes are selected 
      TypedValue[] filList = new TypedValue[2] { new TypedValue((int)DxfCode.Start, "INSERT"), new TypedValue((int)DxfCode.HasSubentities, 1) }; 
      SelectionFilter filter = new SelectionFilter(filList); 
      PromptSelectionOptions opts = new PromptSelectionOptions(); 
      opts.MessageForAdding = "Select block references: "; 
      PromptSelectionResult res = ed.GetSelection(opts, filter); 
      // Do nothing if selection is unsuccessful 
      if (res.Status != PromptStatus.OK) 
       return; 

      SelectionSet selSet = res.Value; 

      ObjectId[] idArray = selSet.GetObjectIds(); 

      PromptPointResult ppr; 
      PromptPointOptions ppo = new PromptPointOptions(""); 
      ppo.Message = "\n Select the place for print output:"; 
      //get the coordinates from user 
      ppr = ed.GetPoint(ppo); 
      if (ppr.Status != PromptStatus.OK) 
       return; 
      Point3d startPoint = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem); 
      Vector3d disp = new Vector3d(0.0, -2.0 * db.Textsize, 0.0); 

      HashSet<string> attValues = new HashSet<string>(); 

      foreach (ObjectId blkId in idArray) 
      { 
       BlockReference blkRef = (BlockReference)tr.GetObject(blkId, OpenMode.ForRead); 
       BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForWrite); 

       //ed.WriteMessage("\nBlock: " + btr.Name); 

       var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); 

       AttributeCollection attCol = blkRef.AttributeCollection; 
       foreach (ObjectId attId in attCol) 
       { 
        AttributeReference attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead); 
        string str = (attRef.TextString); 
        //ed.WriteMessage("\n" + str); 
        if (attValues.Contains(str)) 
         continue; 
        if (btr.Name == "NAL-SCRTAG") 
        { 
         MText mtext = new MText(); 

         mtext.Location = startPoint; 
         string file = acDoc.Name; 
         string str1 = Path.GetFileNameWithoutExtension(file); 

         Match match = Regex.Match(str1, @"^(\w+-[CSDWM]\d+[A-Z]-.)$"); 
         var split = str1.Split('-'); 
         string code = split.First(); 
         string phase = new string(split.ElementAt(1).Skip(1).Take(1).ToArray()); 
         string zone = new string(split.ElementAt(1).Skip(2).Take(1).ToArray()); 

         mtext.Contents = ("Project:" + code + "Phase:" + phase + "Zone:" + zone + "(" + str + ")"); 

         //ed.WriteMessage(text); 
         curSpace.AppendEntity(mtext); 
         tr.AddNewlyCreatedDBObject(mtext, true); 
         db.TransactionManager.QueueForGraphicsFlush(); 

         attValues.Add(str); 

         startPoint += disp; 
        } 
       } 
      } 
      tr.Commit(); 
     } 
     catch (Autodesk.AutoCAD.Runtime.Exception ex) 
     { 
      ed.WriteMessage(("Exception: " + ex.Message)); 
     } 
    } 
} 

Antwort

0

Wenn ich nicht falsch verstehen, was Sie zu tun versuchen, sollten Sie die verschiedenen Aufgaben trennen:

  • Erstellen der Anfang des Textes mit dem Dateinamen zu drucken (die müssen übereinstimmen ein Muster).
  • Fügen Sie die Werte des Attributs der ausgewählten Blöcke am Ende des Textes hinzu.
  • Drucken Sie den Text wie MText in AutoCAD

    [CommandMethod("ATT")] 
    public void ListAttributes() 
    { 
        Document acDoc = Application.DocumentManager.MdiActiveDocument; 
        Editor ed = acDoc.Editor; 
        Database db = acDoc.Database; 
    
        // build the begining of the text from the filename 
        string filename = Path.GetFileNameWithoutExtension(acDoc.Name); 
        string pattern = @"^(?<code>\w+)-[CSDWM](?<phase>\d+)(?<zone>[A-Z])-\w+$"; 
        // pattern description: 
        // ^(?<code>\w+) 'code' named group: one or more word characters at the begining 
        // -    one hyphen (literal) 
        // [CSDWM]   one alphabetic character (C, S, D, W or M) 
        // (?<phase>\d+) 'phase' named group: one or more digits 
        // (?<zone>[A-Z]) 'zone' named group: one alphabetic character 
        // -    one hyphen (literal) 
        // \w+$   one or more world characters at the end 
        Match match = Regex.Match(filename, pattern); 
        if (!match.Success) 
        { 
         ed.WriteMessage("\n The document file name does not match the pattern."); 
         return; 
        } 
        var groups = match.Groups; 
        string text = $"Project:{groups["code"]} Phase:{groups["phase"]} Zone:{groups["zone"]}"; 
    
        // Build a filter list so that only "NAL-SCRTAG" 
        // block references are selected 
        TypedValue[] filList = new TypedValue[2] { new TypedValue(0, "INSERT"), new TypedValue(2, "NAL-SCRTAG") }; 
        SelectionFilter filter = new SelectionFilter(filList); 
        PromptSelectionOptions opts = new PromptSelectionOptions(); 
        opts.MessageForAdding = "Select block references: "; 
        PromptSelectionResult res = ed.GetSelection(opts, filter); 
        // Do nothing if selection is unsuccessful 
        if (res.Status != PromptStatus.OK) 
         return; 
    
        //get the coordinates from user 
        PromptPointOptions ppo = new PromptPointOptions("\n Select the place for print output: "); 
        PromptPointResult ppr = ed.GetPoint(ppo); 
        if (ppr.Status != PromptStatus.OK) 
         return; 
        Point3d startPoint = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem); 
    
        // Start the transaction 
        using (Transaction tr = db.TransactionManager.StartTransaction()) 
        { 
         try 
         { 
          // use a HashSet to avoid duplicated values 
          HashSet<string> attValues = new HashSet<string>(); 
    
          // add the attributes values at the end of the text 
          foreach (ObjectId blkId in res.Value.GetObjectIds()) 
          { 
           BlockReference blkRef = (BlockReference)tr.GetObject(blkId, OpenMode.ForRead); 
           foreach (ObjectId attId in blkRef.AttributeCollection) 
           { 
            AttributeReference attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead); 
            string str = (attRef.TextString); 
            if (attValues.Add(str)) // returns false if attValues already contains str 
            { 
             text += $" ({str})"; // adds the attribute text string at the end of the text 
            } 
           } 
          } 
    
          // add the mText to the current space 
          var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); 
          MText mtext = new MText(); 
          mtext.Location = startPoint; 
          mtext.Contents = text; 
          curSpace.AppendEntity(mtext); 
          tr.AddNewlyCreatedDBObject(mtext, true); 
          tr.Commit(); 
         } 
         catch (Autodesk.AutoCAD.Runtime.Exception ex) 
         { 
          ed.WriteMessage(("Exception: " + ex.Message)); 
         } 
        } 
    }