.NET

Reply
Distinguished Contributor
alex_b
Posts: 393
Registered: ‎08-15-2003
Message 11 of 14 (378 Views)

Re: AcadSelectionSet.AddItems(AcadBlockReference.Explode())

09-04-2012 08:18 PM in reply to: Hallex

Hallex

I can't seem to find a CloneObjects() method in Interop.

If you mean CopyObjects(), all I could find about it is some examples of its use for copying objects between databases and no documentation (also the examples are in VB, knowledge of which I have less than zero).

I understand that there is a problem copying entities from a block definition.

Maybe the (ugly) solution would be

[insert the block; explode it; add the exploded entities to the selection set; wblock the selection set; erase the exploded entities]

Could you help?

Thanks

alex

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 12 of 14 (372 Views)

Re: AcadSelectionSet.AddItems(AcadBlockReference.Explode())

09-04-2012 11:08 PM in reply to: alex_b

I can't test this code on A2004,

just tested them on my A2010, all is Ok here

Try again:

 

       [CommandMethod("copydef")]
       public static void CopyBlockDefToDwg()
       { 
         
           MessageBox.Show("Wait...");
           // change file names to suit:
           string sourcePath = @"C:\Test\WorkingDrawing2.dwg";
           string tempPath = @"C:\Test\atest.dwg";
           AcadApplication acApp = new AcadApplicationClass();
           //open for read only
           acApp.Documents.Open(sourcePath, Type.Missing, Type.Missing);
           AcadDocument AcadDoc = acApp.ActiveDocument;
           acApp.Visible = true;

           AcadBlocks blocks = AcadDoc.Blocks;
           // change block name to suit
           string blockName = "PART";
           AcadBlock bl = blocks.Item(blockName);//gets the block definition of input block name
           // copy blockdefinition to array of AcadObject
           AcadObject[] copy = new AcadObject[1];
           copy[0] = (AcadObject)bl;
           // convert array as single object
           object safe = copy as object;
           //open for read write
           acApp.Documents.Open(tempPath, false, Type.Missing);
           // set source document to be active
           AcadDocument newDoc = acApp.ActiveDocument;
           // prepare arguments for CoptObjects
           object IdPairs = new object();
           // set all arguments as objects
           AcadDoc.CopyObjects(safe, newDoc.Blocks as object, ref IdPairs);
           // close and save destination document
           newDoc.SaveAs(tempPath, Type.Missing, Type.Missing);
           acApp.ActiveDocument = AcadDoc;
           newDoc.Close();
           // close source document without changes
           AcadDoc.Close(Type.Missing, Type.Missing);
           acApp.Quit();
       }

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Distinguished Contributor
alex_b
Posts: 393
Registered: ‎08-15-2003
Message 13 of 14 (364 Views)

Re: AcadSelectionSet.AddItems(AcadBlockReference.Explode())

09-05-2012 11:37 AM in reply to: Hallex

Hallex

The method you posted adds the block definition to the Blocks collection.

I needed it to work like the WBLOCK Autocad command, so I had to modify it a little for that (copy the block definition entityes to the new db model space).

There were some issues with parameters too (probably because the Acad 2004 compatibility requirement).

Anyway, your suggestion works great and Thank you.

Here is the final code:

private void createOutDwg(string tempPath, string blockToOutput)
{
  AcadBlocks blocks = aCadDoc.Blocks;
  AcadBlock bl = blocks.Item(blockToOutput);//gets the block definition
  // copy block definition to array of AcadObject
  int i = bl.Count;
  AcadObject[] copy = new AcadObject[i];
  for (int j = 0; j < i; j++)//iterates thru the block definition
  {
    copy[j] = (AcadObject)bl.Item(j); ;//gets one entity
  }
  // convert array as single object
  object safe = copy as object;
  //create new document
  acApp.Documents.Add(Type.Missing);
  //remember it
  AcadDocument newDoc = acApp.ActiveDocument;
  // prepare arguments for CopyObjects
  object IdPairs = new object();
  // set all arguments as objects
  aCadDoc.CopyObjects(safe, newDoc.ModelSpace as object, ref IdPairs);
  //save and close destination document
  newDoc.SaveAs(tempPath, Type.Missing, Type.Missing);
  //make source document active before closing new doc
  acApp.ActiveDocument = aCadDoc;
  newDoc.Close(Type.Missing, Type.Missing);
}

 I still can't understand why we can't just add entities from the block definition to a selection set and the just Wblock() it. Maybe the Autocad people would like to comment on that.

Thank you again.

alex

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 14 of 14 (361 Views)

Re: AcadSelectionSet.AddItems(AcadBlockReference.Explode())

09-05-2012 11:55 AM in reply to: alex_b

Glad I could help,

Happy coding,

Cheers :smileyhappy:

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community