xarial / xcad

Framework for developing CAD applications for SOLIDWORKS, including add-ins, stand-alone applications, macro features, property manager pages, etc.
https://xcad.net
MIT License
129 stars 27 forks source link

Set Dimension Exceptions #9

Closed emersonbottero closed 4 years ago

emersonbottero commented 4 years ago

I Think mPart.Dimensions["A@Extrude1"]?.SetValue(Dimentions.Asize) should not break the code when there is no "A@Extrude1" in the model, or if it is really necessary we could have a mPart.Dimensions["A@Extrude1"]?.TrySetValue(Dimentions.Asize) that fails silently. Or return an bool informing if it worked or not.

emersonbottero commented 4 years ago

I did an extension for now, But it is in SwDocument. Couldn't check if Dimenstions.any(... it breaks the code too.

 public static bool SetDimentionValue(this SwDocument doc, string DimentionName, double Value)
        {
            if (doc == null)
            {
                Debug.Print($"Can't set a dimention of a document with null reference");
                return false;
            }
            else if ((Dimension)(doc.Model).Parameter(DimentionName) != null)
            {
                doc.Dimensions[DimentionName].SetValue(Value);
                return true;
            }

            Debug.Print($"Dimention {DimentionName} not found in Model {doc.Title}");
            return false;
        }
artem1t commented 4 years ago

I like the idea with TrySetValue/TryGetValue, trying to make it more consistent with IDictionary, i.e. exception if key does not exist (i.e. dimension is not present)

bool TrySetValue(string dimName, double val)

We can add it to the extension method of IXDimensionRepository. It is similar to IXPropertyRepositoryExtension::Set

artem1t commented 4 years ago

Implemented in the preview builds to be merged into release

artem1t commented 4 years ago

Merged to release:

IXDimensionRepository.TrySetDimensionValue IXDimensionRepository.TryGetDimensionValue