.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Howto pick object in one document and then pick in another doc

3 REPLIES 3
Reply
Message 1 of 4
wesbird
355 Views, 3 Replies

Howto pick object in one document and then pick in another doc

Hi,

I'm working on a function to copy custom-data from doc to another doc. First I will pick a AcadObject in DocA, get the custom data, then pick a object in DocB, attach custom data. try this code, but it not work. here is the code.





{code}

[CommandMethod("PickDocX", CommandFlags.Session)]

public static void TEST_PickDocX()

{

ObjectId oid1 = Class1.GetEntityX("Please pick obj1");

ObjectId oid2 = Class1.GetEntityX("Please pick obj2");

}





public static ObjectId GetEntityX(string msg)

{

Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;



ObjectId objId = ObjectId.Null;

// prepare prompt message

PromptEntityOptions prEntOpts = new PromptEntityOptions(msg);



// pick object on the screen

PromptEntityResult prEntRes = ed.GetEntity(msg);



if (prEntRes.Status != PromptStatus.OK)

return ObjectId.Null;



objId = prEntRes.ObjectId;



return objId;

}





{code}
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
3 REPLIES 3
Message 2 of 4
norman.yuan
in reply to: wesbird

You need to switch the active drawing in Acad before asking user to do second pick. Or you do know this but do not know how to switch drawing in acad.

Anyway, here is some code doing that:

{code}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;

using System.IO;

namespace PickInTwoDocs
{
public class MyData
{
public string Drawing1 { get; set; }
public string Drawing2 { get; set; }
public ObjectId PickedInDrawing1 { get; set; }
public ObjectId PickedInDrawing2 { get; set; }
}

public class MyCommand
{
private static MyData _data = null;

[CommandMethod("PickInDocs", CommandFlags.Session)]
public static void RunThisMethod()
{
DocumentCollection docs = Application.DocumentManager;
Document dwg = docs.MdiActiveDocument;

if (docs.Count<2)
{
dwg.Editor.WriteMessage(
"\nThere is only one drawing open. " +
"\nThe program cannot continue!");
return;
}

_data=new MyData();

//Get data from first drawing
_data.Drawing1 = dwg.Name;
_data.PickedInDrawing1 = PickObjectInDwg(dwg);

// Go to next drawing, as long as it
// is not the same drawing
//You should decide which drawing is the
//other drawing in your code
foreach (Document d in docs)
{
if (d.Name.ToUpper() != dwg.Name.ToUpper())
{
// Set this drawing as active drawing, so
// user can do the pick
docs.MdiActiveDocument = d;
dwg=docs.MdiActiveDocument;
break;
}
}

// Get data from the second drawing
_data.Drawing2 = dwg.Name;
_data.PickedInDrawing2 = PickObjectInDwg(dwg);

// Show the result
// You can now use the 2 ObjectIds to do some thing
dwg.Editor.WriteMessage("\n\nPicking result:");
dwg.Editor.WriteMessage("\nPicked object in \"" +
_data.Drawing1 + "\": " +
_data.PickedInDrawing1.ToString());
dwg.Editor.WriteMessage("\nPicked object in \"" +
_data.Drawing2 + "\": " +
_data.PickedInDrawing2.ToString());
dwg.Editor.WriteMessage("\n");
}

private static ObjectId PickObjectInDwg(Document dwg)
{
ObjectId picked = ObjectId.Null;

Editor ed = dwg.Editor;
PromptEntityOptions opt =
new PromptEntityOptions("\nPick an enetity:");
PromptEntityResult res = ed.GetEntity(opt);
if (res.Status == PromptStatus.OK)
{
picked = res.ObjectId;
}

return picked;
}
}
}

{code}

Of course, the 2 ObjecIds you have in hand is only useful when working with their corresponding databases of the two different drawings.

HTH.

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 4
wesbird
in reply to: wesbird

Thank you, Norman.
It works. But I want more, the ideal solution will work like,
there are > 1 drawings opened, user pick object as source in a drawing, then pick another object in current drawing, which should be easy, or any other drawing, any one but not current.
so,
a) I cannot limit the number of opened drawing to 2, could be any number.
b) I cannot do the doc switch in my code, I have allow my user do it, so he/she could pick in any one, current one or any inactive one.
Is this possible?



Thank you very much
Wes
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
Message 4 of 4
norman.yuan
in reply to: wesbird

> {quote:title=wesbird wrote:}{quote}
> a) I cannot limit the number of opened drawing to 2, could be any number.
> b) I cannot do the doc switch in my code, I have allow my user do it, so he/she could pick in any one, current one or any inactive one.
> Is this possible?
>

Of course you do not limit your code only works on 2 open drawings, you should have some way to let user choose which drawing is the source and which drawing is the destination before your code runs.

For example, if in Acad there is more than one drawing opened, your program could show a small form showing all opened drawings, and let user choose source and destination drawings, such as one dropdown list listing all drawings for user to choose a source file, an list box listing all other opened drawings with check box. User then can select one or more other drawings as destination. Once user clicks "OK", you will have the source drawing name and destination drawing name(s). Now you can go ahead with your source drawing searching to get source data and switch to each destination drawing to update something there.

Norman Yuan

Drive CAD With Code

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost