using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using SigartAcadDrawing.Utils;
using Autodesk.AutoCAD.PlottingServices;
namespace SigartAcadDrawing
{
class Sample
{
public void LoadTextStyles(
Database currentAcDb,
string inputFileDirectory,
string inputFileName)
{
Database dbSourceDb = new Database(false, true);
Database dbTargetDb = currentAcDb;
Logs.Trace.TraceInfo("LoadTextStyles()", "textstyle loading started", _activeEditor, IsInDebugOnAcadConsole);
// Open two transactions, the 1rst one for getting blocks from .dwg source file,
// the 2nd one for inserting cloned blocks in the current drawing.
Autodesk.AutoCAD.DatabaseServices.TransactionManager tmSourceTm = dbSourceDb.TransactionManager;
Autodesk.AutoCAD.DatabaseServices.TransactionManager tmTargetTm = dbTargetDb.TransactionManager;
try
{
// Get name of DWG from which to copy blocks;
if (!System.IO.Directory.Exists(inputFileDirectory))
throw new Exception("Le dossier censé contenir le bloc n'existe pas à l'endroit spécifié.");
string sourceFilePath = "";
if (inputFileDirectory.EndsWith(@"\"))
{
sourceFilePath = inputFileDirectory + inputFileName + ".dwg";
}
else
{
sourceFilePath = inputFileDirectory + @"\" + inputFileName + ".dwg";
}
if (!System.IO.File.Exists(sourceFilePath))
throw new Exception("Le fichier censé fournir le bloc n'existe pas.");
AcadFunctions.CloneTextStylesFromOneDbToAnother(dbSourceDb, dbTargetDb, tmSourceTm, sourceFilePath, inputFileName, _activeEditor, IsInDebugOnAcadConsole);
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
Logs.Trace.TraceError(ex, _activeEditor, IsInDebugOnAcadConsole);
}
catch (System.Exception ex)
{
Logs.Trace.TraceError(ex, _activeEditor, IsInDebugOnAcadConsole);
}
finally
{
tmSourceTm.Dispose();
tmTargetTm.Dispose();
dbSourceDb.Dispose();
//dbTargetDb.Dispose();
}
}
///
/// This method comes from another static class
///
///
///
///
///
///
///
///
internal static void CloneTextStylesFromOneDbToAnother(
Database dbSourceDb,
Database dbTargetDb,
Autodesk.AutoCAD.DatabaseServices.TransactionManager tmSourceTm,
string sourceFilePath,
string fileName,
Editor activeEditor,
bool isInDebugOnAcadConsole)
{
try
{
// Read the DWG into the side database
dbSourceDb.ReadDwgFile(sourceFilePath, System.IO.FileShare.Read, true, "");
}
catch (System.Exception)
{
throw new System.Exception("Unable to read drawing source file.");
}
// Create a variable to store the list of block identifiers
ObjectIdCollection oicBlockIds = new ObjectIdCollection();
using (Transaction tCurrentTrans = tmSourceTm.StartTransaction())
{
// Open the block table
TextStyleTable btTextStyleTable =
(TextStyleTable)tmSourceTm.GetObject(dbSourceDb.TextStyleTableId, OpenMode.ForRead, false);
// Check each block in the block table
foreach (ObjectId oiCurrentBtrId in btTextStyleTable)
{
TextStyleTableRecord btr = (TextStyleTableRecord)tmSourceTm.GetObject(oiCurrentBtrId, OpenMode.ForRead, false);
// No need to get the Standard textstyle because it's not replaceable in the new database
if (btr.Name != TextStyleName.Standard.ToString())
{
oicBlockIds.Add(oiCurrentBtrId);
Logs.Trace.TraceInfo("CloneTextStylesFromOneDbToAnother",
"Read "
+ btr.Name
+ " textstyle definition",
activeEditor,
isInDebugOnAcadConsole);
}
btr.Dispose();
}
btTextStyleTable.Dispose();
}
// Copy textstyles from source to destination database
IdMapping mapping = new IdMapping();
dbSourceDb.WblockCloneObjects(oicBlockIds,
dbTargetDb.TextStyleTableId,
mapping,
DuplicateRecordCloning.Replace,
false);
RemoveNotWantedLayer(dbTargetDb);
Logs.Trace.TraceInfo("CloneTextStylesFromOneDbToAnother",
"Loaded "
+ oicBlockIds.Count.ToString()
+ " textstyle definitions from "
+ sourceFilePath
+ " to the current drawing.",
activeEditor,
isInDebugOnAcadConsole);
}
}
}