Problem with null ObjectID of newly created polyline

Problem with null ObjectID of newly created polyline

m.chavet7MJRB
Enthusiast Enthusiast
795 Views
4 Replies
Message 1 of 5

Problem with null ObjectID of newly created polyline

m.chavet7MJRB
Enthusiast
Enthusiast

I'm currently experiencing a problem with a code I wrote in C#. The idea is to merge a series of entities based on an OD value and then transform them into an alignment. The entities are of different kinds (Line, 3D polyline, etc.). I decided to transform them all into 2D polylines in order to merge them correctly.

 

However, depending on the nature of the first entity (here a Line), I create a new entity (a 2D polyline) into which I insert the vertices of the line. The problem is that when I create this new entity, the ObjectID of my new polyline to which other polylines will be added is zero, i.e. the associated ID is zero.

 

In fact, this poses a problem in this part of the code, where the alignment must be created with a non-zero ObjectID (I get an error). Does anyone have a solution for how to associate a new ObjectId or how to create a polyline with a valid objectid?

 

 

 

                else
                {
                    Line line = polyline as Line;
                    

                    Autodesk.AutoCAD.DatabaseServices.Polyline poly2dd = new Autodesk.AutoCAD.DatabaseServices.Polyline();
                    //poly2dd.ObjectId = new ObjectId(new IntPtr(long.Parse("2000")));
                    //ObjectId poly2dd = ee;

                    //ObjectId poly2dd = polyline.ObjectId;

                    if (line != null)
                    {
                        poly2dd.AddVertexAt(0, new Point2d(line.StartPoint.X, line.StartPoint.Y), 0, 0, 0);
                        poly2dd.AddVertexAt(1, new Point2d(line.EndPoint.X, line.EndPoint.Y), 0, 0, 0);
                        poly2dd.Elevation = 0;
                        if (isFirstElement)
                        {
                            isFirstElement = false;
                            continue;
                        }
                        else
                        {
                            //IniPLine.UpgradeOpen();
                            IniPLine.JoinEntity(poly2dd);
                            //poly2dd.ObjectId = line.ObjectId;
                            //IniPLine.DowngradeOpen();
                        }
                    }
                }
            }
        }
    }
    IniPLine.SetDatabaseDefaults();
}


            
// Committing is cheaper than aborting
                    
//btr.AppendEntity(IniPLine);
//tr2.AddNewlyCreatedDBObject(IniPLine, true);

// Get id  of layer "alignments" or use id of current layer  
ObjectId idLayer = db.Clayer;
LayerTable lt = tr2.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
if (lt.Has("alignments")) idLayer = (lt["alignments"]);

// Get id  of the 1st Alignment style or Basic
ObjectId idStyle = civdoc.Styles.AlignmentStyles[0];
if (civdoc.Styles.AlignmentStyles.Contains("Basic")) idStyle = civdoc.Styles.AlignmentStyles["Basic"];

// Get id of the 1st AlignmentLabelSetStyle or Basic
ObjectId idLabelSet = civdoc.Styles.LabelSetStyles.AlignmentLabelSetStyles[0];
if (civdoc.Styles.LabelSetStyles.AlignmentLabelSetStyles.Contains("Basic")) idLabelSet = civdoc.Styles.LabelSetStyles.AlignmentLabelSetStyles["Basic"];

// Set options
PolylineOptions plos = new PolylineOptions();
plos.AddCurvesBetweenTangents = true;
plos.EraseExistingEntities = false;
plos.PlineId = IniPLine.ObjectId;
//plos.PlineId = IniPLine.ObjectId; // /!\!!!
// Create unique name

String nAlign = Alignment.GetNextUniqueName(civdoc.Settings.GetSettings<SettingsCmdCreateAlignmentEntities>().DefaultNameFormat.AlignmentNameTemplate.Value);

//plos.PlineId = IniPLine.ObjectId; // /!\!!!

// Create alignment
ObjectId idAlign = Alignment.Create(civdoc, plos, nAlign, ObjectId.Null, idLayer, idStyle, idLabelSet);

 

 

0 Likes
Accepted solutions (1)
796 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor
Accepted solution

You do not "associate a new ObjectId" to an entity (the Polyline, in your case); nor do you create an entity "with a valid ObjectId". An entity only gets its ObjectId when it is added to the database (e.g. when the entity is appended to Model/Paper space with BlockTableRecord.AppendEntity()).

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

m.chavet7MJRB
Enthusiast
Enthusiast

Hello Norman, 

 

Thank you, now it's ok with that.

 

But now, I get a new error, at the same line: 

 

Application does not support just-in-time (JIT) debugging. See the end of this message for details. ************** Exception Text ************** Autodesk.AutoCAD.Runtime.Exception: eWasOpenForWrite at Autodesk.Civil.DatabaseServices.Alignment.InternalCreateAlignFromPolyline(AcDbDatabase* pDwg, PolylineOptions plineOptions, String alignmentName, AcDbObjectId siteId, AcDbObjectId layerId, AcDbObjectId styleId, AcDbObjectId labelSetId) at Autodesk.Civil.DatabaseServices.Alignment.Create(CivilDocument document, PolylineOptions plineOptions, String alignmentName, ObjectId siteId, ObjectId layerId, ObjectId styleId, ObjectId labelSetId) at AEPCreateProfile.MyCommandsAEP.selectplbyvalue() at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction) at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction) at Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.Invoke()
 
 
I have some difficulties to understand the logic behind these transactions, the "ForRead Write" notions...
0 Likes
Message 4 of 5

ShricharanaB
Advocate
Advocate

Hi, 

I'm not sure what the exact issue is as I can't see your updated code. But this seems to work for me. I hope it helps.

using (Polyline poly2dd = new Polyline())
{
    modelSpace.AppendEntity(poly2dd);
    trans.AddNewlyCreatedDBObject(poly2dd, true);

    poly2dd.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);//your point here
    poly2dd.AddVertexAt(1, new Point2d(10, 10), 0, 0, 0);//your point here
    poly2dd.Elevation = 0;
}

 

0 Likes
Message 5 of 5

ActivistInvestor
Mentor
Mentor

The new polyline cannot be open for write when you create an alignment from it. You can try calling DowngradeOpen() on the polyline before creating the alignment, but that may not work. If it doesn't then you must commit the transaction that you used to create the polyline and start a new transaction, and re-open the new polyline in OpenMode.ForRead, and then create the alignment.

0 Likes