Insert Block from external drawing to current drawing

Insert Block from external drawing to current drawing

swapnil_lokam
Enthusiast Enthusiast
561 Views
3 Replies
Message 1 of 4

Insert Block from external drawing to current drawing

swapnil_lokam
Enthusiast
Enthusiast

I want to write a C# code using AutoCAD .NET API
I want to copy blocks of external drawing into the current drawing but I am unable to find any API's for the same 

0 Likes
Accepted solutions (1)
562 Views
3 Replies
Replies (3)
Message 2 of 4

ActivistInvestor
Mentor
Mentor

Have a look at these posts for an example of how to copy box into another drawing

0 Likes
Message 3 of 4

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You can use or get inspiration from these extension methods.

/// <summary>
/// Imports SymbolTableRecords whose names match the template supplied from the specified file.
/// </summary>
/// <typeparam name="T">Type of SymbolTable.</typeparam>
/// <param name="targetDb">Instance to which the method applies.</param>
/// <param name="sourceFileName">Complete path of the source file.</param>
/// <param name="pattern">WcMatch pattern.</param>
/// <param name="cloning">Input action for duplicate records.</param>
/// <returns>A dictionary containing the names and ObjectIds of the imported records.</returns>
/// <exception cref="ArgumentNullException"></exception>
public static Dictionary<string, ObjectId> ImportRecords<T>(
    this Database targetDb, string sourceFileName, string pattern, DuplicateRecordCloning cloning)
    where T : SymbolTable
{
    if (targetDb is null)
        throw new ArgumentNullException(nameof(targetDb));

    Dictionary<string, ObjectId> records;
    using (var sourceDb = new Database(false, true))
    {
        sourceDb.ReadDwgFile(sourceFileName, FileOpenMode.OpenForReadAndAllShare, false, null);
        using (Transaction tr = sourceDb.TransactionManager.StartOpenCloseTransaction())
        {
            var sourceTable = (T)tr.GetObject(sourceDb.GetTableId<T>(), OpenMode.ForRead);
            records = sourceTable
                .Cast<ObjectId>()
                .Select(id => (SymbolTableRecord)tr.GetObject(id, OpenMode.ForRead))
                .Where(r => Autodesk.AutoCAD.Internal.Utils.WcMatchEx(r.Name, pattern, true))
                .ToDictionary(r => r.Name, r => r.ObjectId);
            var ids = new ObjectIdCollection(records.Values.ToArray());
            var mapping = new IdMapping();
            sourceDb.WblockCloneObjects(ids, targetDb.GetTableId<T>(), mapping, cloning, false);
            tr.Commit();
        }
    }
    using (var tr = targetDb.TransactionManager.StartOpenCloseTransaction())
    {
        var table = (T)tr.GetObject(targetDb.GetTableId<T>(), OpenMode.ForRead);
        return records
            .Where(r => table.Has(r.Key))
            .ToDictionary(r => r.Key, r => table[r.Key]);
    }
}

/// <summary>
/// Gets the ObjectId of the specified SymbolTable.
/// </summary>
/// <typeparam name="T">Type of SymbolTable.</typeparam>
/// <param name="db">Instance to which the method applies.</param>
/// <returns>The ObjectId of the specified SymbolTable.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name ="db"/> is null.</exception>
/// <exception cref="NotImplementedException"></exception>
public static ObjectId GetTableId<T>(this Database db) where T : SymbolTable
{
    if (db is null) throw new ArgumentNullException(nameof(db));

    switch (typeof(T).Name)
    {
        case nameof(BlockTable): return db.BlockTableId;
        case nameof(DimStyleTable): return db.DimStyleTableId;
        case nameof(LayerTable): return db.LayerTableId;
        case nameof(LinetypeTable): return db.LinetypeTableId;
        case nameof(RegAppTable): return db.RegAppTableId;
        case nameof(TextStyleTable): return db.TextStyleTableId;
        case nameof(UcsTable): return db.UcsTableId;
        case nameof(ViewTable): return db.ViewTableId;
        case nameof(ViewportTable): return db.ViewportTableId;
        default:
            throw new NotImplementedException();
    }
}

 

 

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 4

swapnil_lokam
Enthusiast
Enthusiast

Thanks for you valuable time @_gile @ActivistInvestor 

But seems like 
Here it is copy pasting the objects from external drawing to the current drawing. Hence the objects that are pasted to the current drawing are not adjusting the scale by itself. 
When I am manually trying to import these blocks from "Design Centre" in the "View" tab of the ribbon then they are properly getting scaled 
So I am struggling to write the code for the above requirement

0 Likes