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

Trying to mimic LAYUNISO command

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
nam_h_tran
330 Views, 10 Replies

Trying to mimic LAYUNISO command

The code below im trying to mimic the ability of layuniso command. But it not make the layer faded restored back to normal. Although it restore everything normally like layuniso. Im trying to regen model too but seem like it not working.

 

here below the code:

 

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using TrainingAutoCAD.Helpers;



var doc = Application.DocumentManager.MdiActiveDocument;
var ed = Application.DocumentManager.CurrentDocument.Editor;
var db = Application.DocumentManager.CurrentDocument.Database;
using (var tr = db.TransactionManager.StartTransaction())
{
LayerStateManager acLyrStMan;
acLyrStMan = doc.Database.LayerStateManager;

DBDictionary? acDbDict;
acDbDict = tr.GetObject(acLyrStMan.LayerStatesDictionaryId(true),
OpenMode.ForWrite) as DBDictionary;

string sLyrStName = "_LAYISO_STATE";


if (acLyrStMan.HasLayerState(sLyrStName) == true)
{
var rb = acDbDict.XData;
if (rb != null)
{
int index = 0;
foreach (TypedValue typeVal in rb)
{
if (index == 1)
{
string layoutValue = $"*{typeVal.Value.ToString()}";
Application.SetSystemVariable("CLAYOUT", layoutValue);
break;
}
index++;
}
}
acLyrStMan.RestoreLayerState(sLyrStName,
ObjectId.Null,
0,
LayerStateMasks.On |
LayerStateMasks.Locked |
LayerStateMasks.Frozen |
LayerStateMasks.CurrentViewport |
LayerStateMasks.NewViewport);
acLyrStMan.DeleteLayerState(sLyrStName);
acDbDict.Erase();
ed.Regen();
}
tr.Commit();
}

ed.Regen();
10 REPLIES 10
Message 2 of 11
diendev16
in reply to: nam_h_tran

KHó quá bỏ qua đi anh ơi

 

Message 3 of 11
_gile
in reply to: nam_h_tran

Sometimes, it is much more simple to call the native command.

var doc = Application.DocumentManager.MdiActiveDocument;

if (doc.Database.LayerStateManager.HasLayerState("_LAYISO_STATE"))
    doc.Editor.Command("_LAYUNISO");


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 11
nam_h_tran
in reply to: _gile

a, im trying to mimic the layuniso command. But don't find any type of regen or redraw the drawing area like layuniso. When im using editor.regen it will always going with regenerating model... message
Message 5 of 11
18348401357
in reply to: nam_h_tran

yes  the layer faded can't back  is a is a typical problem ,try to use LayerUtilities.RegenLayers Method, In advanced version it's CoreLayerUtilities.RegenLayers, the RegenPending is a property of LayerUtilities

Message 6 of 11
nam_h_tran
in reply to: 18348401357

Thanks, base on you i found this topic:
Topic about layer unlock, faded,hide ( chinese language)  

And here is my code for that: 

Regen Layers :

 

 

           var lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
           ObjectId[] layerIds = lt.Cast<ObjectId>().ToArray();
           RegenLayers(layerIds);        

        public static void RegenLayers(ObjectId[] layerIds)
        {
            dynamic acad = Application.AcadApplication;

            int majorVersion = int.Parse(acad.Version.Split('.')[0]);
            var type = majorVersion >= 21
                ? Assembly.Load("accoremgd")?.GetType("Autodesk.AutoCAD.Internal.CoreLayerUtilities")
                : Assembly.Load("acmgd")?.GetType("Autodesk.AutoCAD.Internal.LayerUtilities");

            var majorInfo = type?.GetMethod("RegenLayers", new[] { typeof(ObjectId[]), typeof(int) });
            var propertyInfo = type?.GetProperties().FirstOrDefault(e => e.Name == "RegenPending");
            var regenPending = (int)(propertyInfo?.GetValue(null) ?? 0);

            majorInfo?.Invoke(null, new object[] { layerIds, regenPending });
        }

 

 

Message 7 of 11
nam_h_tran
in reply to: 18348401357

But i don't understand i can't find any document about LayerUtilities or CoreLayerUtilities in autocad. It should be a document for this somewhere right?
Message 8 of 11
18348401357
in reply to: nam_h_tran

oh I know this man, he is a member of our organization, and he quoted my post in this article.

18348401357_1-1735179163945.png

 

The link that the arrow points to is mine

 

Message 9 of 11
nam_h_tran
in reply to: 18348401357

Again thanks a lots. I was never think about these.
Message 10 of 11
nam_h_tran
in reply to: 18348401357

Hi. Sorry to open this topic again. But you know how to handle it in object arx? (c++). Or how to call the internal library of autocad in object arx?. Here the full verion of code in c++: Im using ads_regen() but it same problem in C#

RestoreLayer() 
{
	AcString          M1 = multilingual_.GetValue(L"M1");
	AcDbDatabase* db = acdbHostApplicationServices()->workingDatabase();
	AcDbLayerStateManager* layerStateManager = db->getLayerStateManager();
	
	try
	{
		if (layerStateManager->hasLayerState(LAYER_NAME) == true)
		{
			AcDbDictionary* dic_layerstate;
			Acad::ErrorStatus  es = acdbOpenObject(dic_layerstate, layerStateManager->layerStatesDictionaryId(true), kForRead);
			if (es == Acad::eOk && dic_layerstate != nullptr)
			{
				GetLayoutAndViewPort(dic_layerstate);
			}
			RestoreData(layerStateManager, db);
			layerStateManager->deleteLayerState(LAYER_NAME);
			ads_regen();
		}
		else
		{
			throw acutPrintf(multilingual_.GetValue(L"M2"));
		}
	}
	catch (...)
	{
		throw;
	}
}

 

 

Message 11 of 11
nam_h_tran
in reply to: nam_h_tran

I found out the answer for the RegenLayer in ObjectArx (c++) too:

 

void __stdcall acedRegenLayers(
	class AcArray<class AcDbObjectId, class AcArrayMemCopyReallocator<class AcDbObjectId>> const&,
	int);

static Acad::ErrorStatus GetLayerObjectIdArray(AcDbObjectIdArray& objIds, bool External = true)
{
	Acad::ErrorStatus     es = Acad::eOk;
	AcDbLayerTablePointer pTable(acdbCurDwg()->layerTableId());
	if (pTable.openStatus() != Acad::eOk)
		return es;

	AcDbLayerTableIterator* pIter(nullptr);
	es = pTable->newIterator(pIter);
	if (es != Acad::eOk)
		return es;

	AcDbLayerTableRecord* pRecord(nullptr);
	for (pIter->start(); !pIter->done(); pIter->step())
	{
		es = pIter->getRecord(pRecord, AcDb::kForRead);
		if (es == Acad::eOk)
		{
			if (pRecord->isDependent() && External)
			{
				objIds.append(pRecord->objectId());
			}
			else
				objIds.append(pRecord->objectId());
		}
		pRecord->close();
	}
	delete pIter;

	return es;
}

static void RegenLayer()
{
	AcDbObjectIdArray layerIds{};
	GetLayerObjectIdArray(layerIds);
	acedRegenLayers(layerIds, 0);
}

 

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report