I've been reading this thread with interest, and was baffled at the lack of a better/easier way to do a .NET "MATCHPROP". I've done the "manual" route any number of times, too. Finally came up with this today, which uses the protocol extension classes that make "MATCHPROP" tick.
public enum MatchPropFlags //dbmatch.h
{
ColorFlag = 0x1,
LayerFlag = 0x2,
LtypeFlag = 0x4,
ThicknessFlag = 0x8,
LtscaleFlag = 0x10,
TextFlag = 0x20,
DimensionFlag = 0x40,
HatchFlag = 0x80,
LweightFlag = 0x100,
PlotstylenameFlag = 0x200,
PolylineFlag = 0x400,
ViewportFlag = 0x800,
TableFlag = 0x1000,
MaterialFlag = 0x2000,
ShadowDisplayFlag = 0x4000,
MultileaderFlag = 0x8000,
TransparencyFlag = 0x10000,
SetAllFlagsOn = 0x1FFFF,
};
[CommandMethod("mymatch")]
public void MyMatchProp()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptEntityResult per1 = ed.GetEntity("Pick the source ent: ");
PromptEntityResult per2 = ed.GetEntity("Pick the dest ent: ");
using (Transaction t = doc.Database.TransactionManager.StartTransaction())
{
Entity src = t.GetObject(per1.ObjectId, OpenMode.ForRead) as Entity;
Entity dest = t.GetObject(per2.ObjectId, OpenMode.ForWrite) as Entity;
MatchProps(src, dest);
t.Commit();
}
}
public void MatchProps(Entity src, Entity dest)
{
// make sure we have acmatch.arx
HostApplicationServices.Current.LoadApplication("AcadMatch",
ApplicationLoadReasons.OnLoadRequest, false, false);
IntPtr _propertyMatcher = IntPtr.Zero;
RXClass srcClass = src.GetRXClass();
RXClass destClass = dest.GetRXClass();
if (srcClass.IsDerivedFrom(destClass) || destClass.IsDerivedFrom(srcClass))
{
// Unmanaged AcDbMatchProperties protocol extension class specific to dest entity
_propertyMatcher = dest.QueryX(RXClass.GetClass(typeof(MatchProperties)));
}
else
{
// Unmanaged generic AcDbMatchProperties protocol extension class
_propertyMatcher = RXClass.GetClass(typeof(Entity)).QueryX(RXClass.GetClass(typeof(MatchProperties)));
}
if (_propertyMatcher == IntPtr.Zero) return;
using (MatchProperties propertymatcher = MatchProperties.Create(_propertyMatcher, false) as MatchProperties)
{
int matchFlags = (int)MatchPropFlags.SetAllFlagsOn;
propertymatcher.CopyProperties(src, dest, matchFlags);
}
}