Преобразование массива Python в C # и возвращение значений - PullRequest
0 голосов
/ 01 ноября 2018

Я конвертирую скрипт на Python в C # и мне нужна помощь. У меня просто нет опыта работы с Python. Эти типы массивов являются совершенно новыми для меня.

У меня проблемы со второй до последней строки, var posVec = dSorted[0][1];, а также с последней строкой: return posVec;.

Каков фактический тип переменной var posVec?

Также я пытаюсь вернуть posVec, который должен быть Vector3d, но я получаю эту ошибку:

Невозможно неявно преобразовать тип 'double' в 'Rhino.Geometry.Vector3d'

Что я делаю не так? Спасибо!

Python:

posVec = dSorted[0][1]
return posVec

Полный метод Python:

def getDirection(self):
    #find a new vector that is at a 90 degree angle

    #define dictionary
    d = {}
    #create an list of possible 90 degree vectors
    arrPts = [(1,0,0), (-1,0,0), (0,1,0), (0,-1,0)]
    vec = self.vec
    vec = rs.VectorUnitize(vec)

    #find the distance between the self vec
    #position and one of the 4 90 degree vectors
    #create a dictionary that matches the distance with the 90 degree vector
    for i in range(4):
        dist = rs.Distance(vec, arrPts[i])
        d[dist] = arrPts[i]
    #sort the dictionary.  This function converts it to an array
    #sort by the distances, "value"/item 0
    dSorted = sorted(d.items(), key=lambda value: value[0])

    #select the second item in the array which is one of the 90 degree vectors
    posVec = dSorted[0][1]
    return posVec

Полный метод C #, который я до сих пор переписывал:

    // find a new vector that is at a 90 degree angle
    public Vector3d GetDirection()
    {
        // define dictionary
        Dictionary<double, Vector3d> d = new Dictionary<double, Vector3d>();

        Vector3d[] arrPts = new Vector3d[] {
            new Vector3d(1, 0, 0),
            new Vector3d(-1, 0, 0),
            new Vector3d(0, 1, 0),
            new Vector3d(0, -1, 0),
            new Vector3d(0, 0, 1),
            new Vector3d(0, 0, -1) };

        _vec = Vec;
        _vec.Unitize();

        // find the distance between the self vec position and one of the 6 90 degree vectors
        // create a dictionary that matches the distance with the 90 degree vector

        for (int i = 0; i < arrPts.Length; i++)
        {
            double dist = Math.Sqrt(
                ((_vec.X - arrPts[i].X) * (_vec.X - arrPts[i].X)) +
                ((_vec.Y - arrPts[i].Y) * (_vec.Y - arrPts[i].Y)) +
                ((_vec.Z - arrPts[i].Z) * (_vec.Z - arrPts[i].Z)));

            d.Add(dist, arrPts[i]);
        }

        Vector3d[] dSorted = d.Values.ToArray();
        var posVec = dSorted[0][1];
        return posVec;
    }

1 Ответ

0 голосов
/ 15 марта 2019

Я опаздываю на вечеринку, но в любом случае, если она будет служить кому-то в будущем ...

Вы создаете массив Vector3d(dSorted) из ваших значений dictionary(d), но вы пытаетесь привести его к var posVec, который должен быть Vector3d с использованием dSorted[0][1]. Это нотация для зубчатого массива, но вы объявили dSorted массивом Vector3d.

Итак, чтобы получить доступ к одному из его элементов, просто используйте dSorted[0].

...