I'm trying to create a TIN Surface in the below function and later I want to extrude it into a solid of depth 2 meters.
While debugging the code, I have noticed that my code runs fine butt I still can't see new surfaces being created on C3D Document. Do I have to append the surface to the Model Space and then also add it to the transaction? If yes, i tried doing that as well but then I am getting error - Object already in database.
Here is my code:
//Create TIN Surface and define its Breaklines and Boundaries.
public ObjectId CreateTinSurface(Polyline2d boundary, bool ExtrudeSolid = false, double DepthOfSolid = 2.0)
{
var surfaceId = new ObjectId(); //Placeholder for surfaceId.
using (var db = Application.DocumentManager.MdiActiveDocument.Database)
using (var trans = db.TransactionManager.StartTransaction())
using (var blockTable = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead))
using (var modelSpace = (BlockTableRecord)trans.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead))
{
try
{
//Create an empty TIN surface
surfaceId = TinSurface.Create(db, "TempVisSurface_"+boundary.BlockName.Trim().ToString());
//Open the TIN surface
TinSurface surface = (TinSurface)trans.GetObject(surfaceId, OpenMode.ForWrite);
//Surface Breaklines Definition
//BoundarySet will also serve as a BreaklinesSet here, since the surface is empty anyways.
ObjectIdCollection breaklineSet = new ObjectIdCollection();
breaklineSet.Add(boundary.ObjectId);
surface.BreaklinesDefinition.AddStandardBreaklines(breaklineSet, 1.0, 1.0, 1.0, 0);
////Surface Boundary Definition
ObjectIdCollection boundaryIdSet = new ObjectIdCollection();
boundaryIdSet.Add(boundary.ObjectId);
surface.BoundariesDefinition.AddBoundaries(boundaryIdSet, 1.0, Autodesk.Civil.SurfaceBoundaryType.Outer, true);
//Conditionally also convert the surface into solid with given depth
//Ideally, Solid will be created with depth 2m.
if (ExtrudeSolid == true)
surface.CreateSolidsAtDepth(DepthOfSolid, surface.Layer, 0);
//***My question is: do i need to run the below code??
//modelSpace.AppendEntity(surface);
//trans.AddNewlyCreatedDBObject(surface, true);
//***End of question
}
catch (Exception ex)
{
Application.ShowAlertDialog("Error creating TIN Surface.\n" + ex.Message);
}
}
return surfaceId;
}
Thanks @hosneyalaa. I will end the transaction, but that still doesn't solve the problem.
Also btw, since the transaction is in using-statement, I don't think I necessarily need to close it but I'm not sure. It sounds like a good idea to commit the transaction manually.
C3D handles adding the Surface to the database.
@heyimsp12 wrote:
Thanks @hosneyalaa. I will end the transaction, but that still doesn't solve the problem.
Also btw, since the transaction is in using-statement, I don't think I necessarily need to close it but I'm not sure. It sounds like a good idea to commit the transaction manually.
If you don't Commit the transaction, it is disposed when exiting the Using statement.
I think you will need to Rebuild the Surface prior to creating the solids.
Can't find what you're looking for? Ask the community or share your knowledge.