0

我们有 Autodesk Inventor 并创建了一个 C# 插件。

在此我们循环遍历所有对象,如下所示:

 foreach (CurrencyAPIv2.IAssetInstance s in objects)
 {
    var c = s.NativeObject as ComponentOccurrence;
 }

我们怎样才能得到一个对象的连接对象(捕捉到它的那个)。还有它连接到的连接器的信息(捕捉)

4

2 回答 2

0

迈克尔·纳瓦拉:

你可以说得更详细点吗?CurrencyAPIv2.IAssetInstance 不是 Inventor API 的成员

使用 CurrencyAPIv2 = Autodesk.Factory.PublicAPI.Currency.v2;

于 2021-08-12T09:48:27.217 回答
0

我们使用 XML 文件解决了这个问题,这些文件也可以在 AttributeHelper 中查看:

        void LoadConnectors(Document document, List<ConnectionElement> connections, List<ConnectedComponent> components)
        {
            var am = document.AttributeManager;

            var d = new Dictionary<string, string>();
            var entities = am.FindObjects("*", "*");
            foreach (var e in entities)
            {
                try
                {
                    dynamic o = e;
                    foreach (AttributeSet attrS in o.AttributeSets)
                    {
                        foreach (Inventor.Attribute a in attrS)
                        {
                            d.Add(o.Name, a.Value as string);
                        }
                    }
                }
                catch(Exception)
                { }
            }

            foreach (var element in d)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(element.Value);

                var connectionNodes = xmlDoc.SelectNodes("//ObjectData[@Type='42']");
                var componentNodes = xmlDoc.SelectNodes("//ObjectData[@Type='38']");

                // Verbindungen
                if (connectionNodes.Count > 0)
                {
                    var link1Node = xmlDoc.SelectSingleNode("//ObjectData/Link1") as XmlElement;
                    var link2Node = xmlDoc.SelectSingleNode("//ObjectData/Link2") as XmlElement;
                    var connection = new ConnectionElement();
                    connection.Name = element.Key;
                    connection.Link1 = link1Node.GetAttribute("VAL");
                    connection.Link2 = link2Node.GetAttribute("VAL");
                    connections.Add(connection);
                }
                // Komponenten
                else if (componentNodes.Count > 0)
                {
                    var componentConnectorNodes = xmlDoc.SelectNodes("//ObjectData/Connector");
                    var componentConnectors = new List<ComponentConnector>();
                    foreach (XmlNode cc in componentConnectorNodes)
                    {
                        var idNode = cc.SelectSingleNode("Id") as XmlElement;
                        var displayNameNode = cc.SelectSingleNode("DisplayName") as XmlElement;

                        var connector = new ComponentConnector();
                        connector.DisplayName = displayNameNode.GetAttribute("VAL");
                        connector.Id = idNode.GetAttribute("VAL");
                        componentConnectors.Add(connector);
                    }
                    
                    if (components.FirstOrDefault(x => x.Name == element.Key) != null)
                    {
                        components.FirstOrDefault(x => x.Name == element.Key).Connectors.AddRange(componentConnectors);
                    }
                    else
                    {
                        var component = new ConnectedComponent();
                        component.Name = element.Key;
                        component.Connectors = new List<ComponentConnector>();
                        component.Connectors.AddRange(componentConnectors);
                        components.Add(component);
                    }
                }
            }
        }

于 2021-10-12T13:40:59.313 回答