Create Entity on Given Layout of External Drawing

richardpangburn
Enthusiast
Enthusiast

Create Entity on Given Layout of External Drawing

richardpangburn
Enthusiast
Enthusiast

I've narrowed down a question I had in another thread, apologies if it seems like I'm spamming the forum...

 

Whenever I insert -any- new object into a specific layout BlockTableRecordId, the entire layout seems to become that object. In other words, it seems like I am replacing the BlockTableRecord belonging to that Layout with my appended object.

 

Here is my code that pertains to getting the layout and appending the circle:

 

BlockTableRecord paperSpace = null;
DBDictionary layouts = (DBDictionary)externalTransaction.GetObject(externalDatabase.LayoutDictionaryId, OpenMode.ForRead);
foreach (DBDictionaryEntry layout in layouts)
{
Layout externalLayout = (Layout)externalTransaction.GetObject(layout.Value, OpenMode.ForRead);
if (destinationLayout.LayoutName != "Model" && destinationLayout.TabOrder == 1)
{
paperSpace = (BlockTableRecord)externalTransaction.GetObject(externalLayout.BlockTableRecordId, OpenMode.ForRead);
}
}



using(Circle circle = new Circle())
{
circle.SetDatabaseDefaults();
circle.Radius = 5;
circle.Center = new Point3d(2, 3, 0);

paperSpace.UpgradeOpen();
paperSpace.AppendEntity(circle);
externalTransaction.AddNewlyCreatedDBObject(circle, true);
}

 

Now in this situation the first paper space layout tab in my external drawing will just be that circle, with the page border graphics behind it. I'm sorry but I can't explain it any better, it's just weird.

 

Any help would be greatly appreciated.

0 Likes
Reply
323 Views
4 Replies
Replies (4)

norman.yuan
Mentor
Mentor

@richardpangburn wrote:

I've narrowed down a question I had in another thread, apologies if it seems like I'm spamming the forum...

....

Now in this situation the first paper space layout tab in my external drawing will just be that circle, with the page border graphics behind it. I'm sorry but I can't explain it any better, it's just weird.

 

Any help would be greatly appreciated.


I don't quite understand what you expect the code does: doesn't the code just draw a circle on the layout as it should? Why do you have to mention "external drawing"? Do you mean the same code work differently with drawing opened in Acad editor and with drawing opened as side database?

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes

richardpangburn
Enthusiast
Enthusiast

Edit: Okay I am embarrassed now. I have found out what's happening. When I append my object into paperspace, the page setup is modified so the scale is "fit to paper" and it's fitting the sheet to the object that's been inserted. I deleted my diatribe below now that I've identified the issue.

 

So I guess my question is...how do I stop that?

 

Edit 2: And I've solved the problem. My test drawing was a brand new empty drawing and had absolutely no page setup on the layout that I was inserting the object into. Turns out if you're inserting an entity into a layout in such a state, the "page setup" will just go into "fit to paper" around that object. 0,0 of that page setup becomes the insertion point of the block in my case, even if I specify a different point.

 

When I updated my test drawing to have a basic page setup (DWG to PDF, Arch Expand D 24x36, Plot Layout, 1:1 scale) the object came in exactly as expected.

 

I feel like I need to kick myself, but at least I learned something...

 

0 Likes

norman.yuan
Mentor
Mentor

Well, it looks to me is that you probably have not drawn anything in layout/paperspace and not fully understand what Paperspace and layout are.

 

Paperspace is like a unlimited-sized paper where you can draw something as small as an atom, or as larger as the universe (the same as ModelSpace, in this sense), while the layout presented in the Paperspace is just a VISUAL HINT/SUGGESTION of a pre-defined paper size, where you better place your presentable geometries with that area to make business sense. It is the duty of you (or your code) to make sure your presentable geometries to be drawn within the area of the layout, or choose suitable size for the layout to contain the presentable geometries. But there is nothing to prevent you/your code to draw things beyond the layout area, it is entirely up to you.

 

That is, your code does exactly as it should do. If you want to fit the blockreference, that is inserted, to the layout, you either choose a layout size that is large enough, or you scale the block reference down.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes

richardpangburn
Enthusiast
Enthusiast

Thanks Norman. I do understand what paper space is and what is happening in it, and on the layout's page setup. I have been an AutoCAD user for many years so that part of it was never in question. The problem I had was more from the lack of experience in appending objects into a layout's paperspace from code and not realizing that there was a specificbehavior that I need to either treat in code, or have set up correctly before trying to append an entity into paperspace.

 

What I did not know at the time was that when inserting an entity into paperspace that has "none" defined for the page setup in AutoCAD 2017 or 2020, the plot area changes to "extents" and the Plot Scale is set to "Fit to Paper". I don't know if this is normal behavoir, all I can say is I've tested it a few times this morning on brand new drawings and got the same result each time. The layout page setup was "none" basically the default page setup, the plot area was set to "Layout" and the Plot Scale is 1:1. After running my code and appending the object the above situation occurs with the page setup being modified.

 

I misinterpreted that behavior as some kind of problem with the way I was appending the object. I thought I was breaking the drawing or doing something horribly wrong. I did not realize that AutoCAD was changing the default plot settings assigned to that layout. I hadn't thought to take a look at the page setup of the layout until I was putting together my response to you last night and realized what was happening.

 

Essentially, I got so caught up in what I was doing in code that I forgot to check the basics first.

 

The solution for me was to insert the object into a paperspace who's layout page setup is already defined, or alternatively, I need to set up the layout plot settings (page setup) myself in code if there is none assigned.

 

Anyway I take problems like this as a learning experience as I continue in my learning process as a developer, but I apologize if you feel like I've wasted your time or posted junk on the forum.

0 Likes