Ejemplo n.º 1
0
        //
        // Récupération des informations sur les blocks dans le fichier excel spécifié
        //
        public void GetInfoExcel(string path)
        {
            string ext = GetExtension(path);
            string filePath = path;
            string connectionString;
            if (ext.Equals(".xlsx"))
                connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=YES\";";
            else
                connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0\";";
            var connection = new OleDbConnection(connectionString);
            const string cmdText = "SELECT * FROM [Feuil1$]";
            var command = new OleDbCommand(cmdText, connection);
            command.Connection.Open();
            var reader = command.ExecuteReader();

            // On vérifie qu'il y a bien des lignes dans le fichier.
            if (reader != null && reader.HasRows)
            {
                var bc = new BoxCollection { ArrayBox = new List<Box>() };
                while (reader.Read())
                {
                    // On ajoute à la liste tous les blocs trouvés dans le fichier.
                    bc.ArrayBox.Add(SerializeExcel(reader).Clone() as Box);
                }
                // Ecriture du fichier XML avec les informations récoltées.
                var serializer = new XmlSerializer(typeof(BoxCollection));
                TextWriter textWriter = new StreamWriter(@"C:\Box.xml");
                serializer.Serialize(textWriter, bc);
                textWriter.Close();
            }
        }
Ejemplo n.º 2
0
 public void DeSerializeFile(string path)
 {
     try
     {
     var serializer = new XmlSerializer(typeof(BoxCollection));
     var reader = new StreamReader(path);
     _boxs = (BoxCollection)serializer.Deserialize(reader);
     reader.Close();
     GetDocument(_boxs);
     GetDocument2(_boxs);
     }
     catch (Exception)
     {
         MessageBox.Show("File not found.");
     }
 }
Ejemplo n.º 3
0
 private void GetDocument2(BoxCollection boxArray)
 {
     for (int y = 0; y <= _rLevels; y++)
     {
         for (int x = 0; x < _rRacks; x++)
         {
             try
             {
                 Document doc = Application.DocumentManager.MdiActiveDocument;
                 Database db = doc.Database;
                 Editor ed = doc.Editor;
                 Transaction tr = db.TransactionManager.StartTransaction();
                 using (tr)
                 {
                     // Get the block table from the drawing
                     var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                     string blkName = checkName(boxArray, bt, x, ed);
                     // Create our new block table record...
                     var btr = new BlockTableRecord { Name = blkName };
                     // ... and set its properties
                     // Add the new block to the block table
                     using (Application.DocumentManager.MdiActiveDocument.LockDocument())
                     {
                         bt.UpgradeOpen();
                         Application.UpdateScreen();
                         ObjectId btrId = bt.Add(btr);
                         tr.AddNewlyCreatedDBObject(btr, true);
                         // Add some lines to the block to form a square
                         // (the entities belong directly to the block)
                         DBObjectCollection ents = RacksOfLines(_rHeight, _rDeep);
                         foreach (Entity ent in ents)
                         {
                             btr.AppendEntity(ent);
                             tr.AddNewlyCreatedDBObject(ent, true);
                         }
                         // Add a block reference to the model space
                         var ms =
                             (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                         var br = new BlockReference(new Point3d(0, x * _rDistance, y * _rHeight), btrId);
                         ms.AppendEntity(br);
                         tr.AddNewlyCreatedDBObject(br, true);
                         // Commit the transaction
                         tr.Commit();
                         // Report what we've done
                         ed.WriteMessage("\nCreated block named \"{0}\" containing {1} entities.", blkName,
                                         ents.Count);
                     }
                 }
             }
             catch (Exception e)
             {
                 MessageBox.Show("There is a problem in block creation, here is the error : " + e);
             }
         }
     }
 }
Ejemplo n.º 4
0
 //
 // Vérification de la non-utilisation du nom des blocks
 //
 private string checkName(BoxCollection boxArray, BlockTable bt, int x, Editor ed)
 {
     string blkName = "";
     do
         try
         {
             blkName = boxArray.ArrayBox[x].BlckName + _id.ToString(CultureInfo.InvariantCulture);
             while (bt.Has(blkName))
             {
                 _id++;
                 blkName = boxArray.ArrayBox[x].BlckName + _id.ToString(CultureInfo.InvariantCulture);
             }
         }
         catch
         {
             ed.WriteMessage("\nInvalid block name.");
         } while (blkName == "");
     return blkName;
 }