Update insulation thickness, insulation type and service via .NET API using PnPTable P3dLineGroup

Update insulation thickness, insulation type and service via .NET API using PnPTable P3dLineGroup

eduardo_salvador02
Enthusiast Enthusiast
427 Views
2 Replies
Message 1 of 3

Update insulation thickness, insulation type and service via .NET API using PnPTable P3dLineGroup

eduardo_salvador02
Enthusiast
Enthusiast

Hello, I'm trying to update 3 specific properties via the .NET API for Plant 3D 2025. However, I'm having some problems, when executing my method that updates the lineGroupId with the new properties for the first time in the file, it works very well, the problem is that , if I close the file after running, and open it again and try to run it again or even trace a pipe using "RoutePipe", it gives an error when adding the objects related to the Pipe.


One of the most common errors I saw: Cannot create a part record in the project database.


Can anyone identify what my error is in this method below?

private static void ApplyPipeSettingsToAllObjects(ObjectId pipeId, Dictionary<string, string> pipeSettings, DataLinksManager dlm, Database db, Editor ed)
{
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        try
        {
            if (pipeId != ObjectId.Null)
            {
                if (pipeId.IsErased)
                {
                    return;
                }

                Pipe pipe = tr.GetObject(pipeId, OpenMode.ForRead) as Pipe;
                PipeInlineAsset pipeInlineAsset = tr.GetObject(pipeId, OpenMode.ForRead) as PipeInlineAsset;

                if (pipe == null && pipeInlineAsset == null)
                {
                    return;
                }

                if (pipe != null || pipeInlineAsset != null)
                {
                    try
                    {
                        int rowId = dlm.FindAcPpRowId(pipeId);

                        if (rowId == -1)
                        {
                            ed.WriteMessage("\nrowId inválido. Não foi possível associar o pipe ao grupo.");
                            return;
                        }

                        PnPDatabase db1 = dlm.GetPnPDatabase();
                        PnPTable tbl = db1.Tables["P3dLineGroup"];

                        PnPRowIdArray lineGroupId = dlm.GetRelatedRowIds("P3dLineGroupPartRelationship", "Part", rowId, "LineGroup");

                        if (lineGroupId == null || lineGroupId.Count == 0)
                        {
                            ed.WriteMessage("\nNenhum grupo de linha relacionado encontrado.");
                            return;
                        }

                        int lineGroupRowId = lineGroupId.First.Value;

                        if (modifiedLineGroupIds.Contains(lineGroupRowId))
                        {
                            return;
                        }

                        string sStatement = "PnPID=" + lineGroupRowId;
                        PnPRow[] rows = tbl.Select(sStatement);

                        if (rows.Length > 0)
                        {
                            foreach (PnPRow Row in rows)
                            {
                                string newInsulationThickness = pipeSettings.ContainsKey("Isolamento") ? pipeSettings["Isolamento"] : string.Empty;
                                string newInsulationType = pipeSettings.ContainsKey("TipIsolamento") ? pipeSettings["TipIsolamento"] : string.Empty;
                                string newService = pipeSettings.ContainsKey("Service") ? pipeSettings["Service"] : string.Empty;

                                bool rowChanged = false;

                                try
                                {
                                    Row.BeginEdit();

                                    if (Row["InsulationThickness"] != null && !string.IsNullOrEmpty(newInsulationThickness) && Row["InsulationThickness"] != newInsulationThickness)
                                    {
                                        Row.SetPropertyValue("InsulationThickness", newInsulationThickness);
                                        rowChanged = true;
                                    }

                                    if (Row["InsulationType"] != null && !string.IsNullOrEmpty(newInsulationType) && Row["InsulationType"] != newInsulationType)
                                    {
                                        Row.SetPropertyValue("InsulationType", newInsulationType);
                                        rowChanged = true;
                                    }

                                    if (Row["Service"] != null && !string.IsNullOrEmpty(newService) && Row["Service"] != newService)
                                    {
                                        Row.SetPropertyValue("Service", newService);
                                        rowChanged = true;
                                    }
                                }
                                catch (System.Exception ex)
                                {
                                    MessageBox.Show($"Erro ao tentar atualizar InsulationThickness, InsulationType e Service.\nErro: {ex}", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                                finally
                                {
                                    if (rowChanged)
                                    {
                                        Row.EndEdit();
                                    }
                                    else
                                    {
                                        Row.CancelEdit();
                                    }
                                }
                            }

                            try
                            {
                                dlm.AcceptChanges();
                            }
                            catch (System.Exception ex)
                            {
                                ed.WriteMessage($"\nErro ao aplicar mudanças no DataLinksManager: {ex.Message}\n");
                            }

                            if (!modifiedLineGroupIds.Contains(lineGroupRowId))
                            {
                                modifiedLineGroupIds.Add(lineGroupRowId);
                            }
                        }
                        else
                        {
                            ed.WriteMessage("\nNenhuma linha encontrada na tabela P3dLineGroup.");
                        }
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show($"Erro ao tentar atualizar InsulationThickness e InsulationType: {ex}", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

            tr.Commit();
        }
        catch (System.Exception ex)
        {
            tr.Abort();
            MessageBox.Show($"Ocorreu um erro inesperado ao tentar setar as propriedades para o objeto Pipe.\nErro: {ex}", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
0 Likes
428 Views
2 Replies
Replies (2)
Message 2 of 3

sreeparna_mandal
Autodesk
Autodesk

Hi @eduardo_salvador02 , can you please expand on what file are you talking about when you said " if I close the file after running, and open it again and try to run it again". Can you please expand more on the issue so I can communicate with the Plant team...

 

0 Likes
Message 3 of 3

eduardo_salvador02
Enthusiast
Enthusiast

Hi, I was referring to any DWG file. But today I made a breakthrough.

It turns out that when I finish executing my method that changes these properties for the LineGroupId, if I close the DWG file without saving, and open it again later, it apparently corrupts the file, I believe it must be because the information does not match the of the database.

Currently, I placed it at the end of my method, to save the file, so I didn't have any more problems. However, it would not be ideal for me to always save the file at the end, as the user will not always want to save.

0 Likes