/************************************* perform INSERT targetBlockName=sourcePath params: * targetBlockName: target block * sourcePath: path to external source * replace: if set, replace all entities in target, else keep target and add entities from source notes: - if the operation adds ATTDEFs to the target (or modifies existing ones) ATTSYNC has to be performed before the results are visible (until then, the new attributes do not show even in LIST command). *************************************/ private void insertEQ(string targetBlockName, string sourcePath, bool replace) { //load source dwg (in ReadOnly mode to make sure nothing happens to it) AcadDocument sourceDoc = acApp.Documents.Open(sourcePath, true, null); if (sourceDoc == null) { MessageBox.Show("can't open " + sourcePath + ".\nAborting"); return; } //read all its entities: copy block definition to array of AcadObject int i = sourceDoc.ModelSpace.Count; AcadObject[] copy = new AcadObject[i]; for (int j = 0; j < i; j++)//iterates thru the block definition { copy[j] = (AcadObject)sourceDoc.ModelSpace.Item(j); ;//gets one entity } object safe = copy as object;// convert array as single object //if replace == true, erase all entities from target AcadBlock trg = aCadDoc.Blocks.Item(targetBlockName); if (replace) { int j = trg.Count; for (int ct = 0; ct < j; ct ++) { trg.Item(0).Delete(); } } //copy entities to target //copy entities to current doc's ModelSpace aCadDoc.Activate(); long lastCount = aCadDoc.ModelSpace.Count - 1; object IdPairs = new object(); sourceDoc.CopyObjects(safe, aCadDoc.ModelSpace, ref IdPairs); long postCount = aCadDoc.ModelSpace.Count - 1; int copyLength = copy.Length; AcadEntity[] addObjs = new AcadEntity[copyLength];// { }; object addObject = new object(); long a = 0; for (long n = lastCount + 1; n <= lastCount + copy.Length; n++) { addObjs[a] = (AcadEntity)aCadDoc.ModelSpace.Item(n); a++; } addObject = addObjs; // prepare arguments for CopyObjects object nPairs = new object(); // copy all objects from src to trg (set all arguments as objects) aCadDoc.CopyObjects(addObject, trg as object, ref nPairs); //erase temporary objects foreach (AcadEntity delobj in addObjs) { delobj.Delete(); } //perform regen so changes become visible aCadDoc.Regen(AcRegenType.acAllViewports); //close and release source sourceDoc.Close(Type.Missing, Type.Missing); }