Пример #1
0
        private void btnMerge_Click(object sender, EventArgs e)
        {
            var models = new List <SvfModel>();

            foreach (SvfModel item in svfModelBindingSource)
            {
                if (item == null)
                {
                    continue;
                }
                models.Add(item);
            }
            if (models.Count < 1)
            {
                MessageBox.Show(this, @"The source model list is empty!", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (string.IsNullOrEmpty(txtOutput.Text))
            {
                MessageBox.Show(this, @"Please Select Output Path!", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            using (var session = LicenseConfig.Create())
            {
                if (session.IsValid == false)
                {
                    LicenseConfig.ShowDialog(session, this);
                    return;
                }

                using (new ProgressHelper(this, @"Merging ..."))
                {
                    try
                    {
                        var sw        = Stopwatch.StartNew();
                        var targetDoc = new SvfDocument();
                        var docs      = new List <SvfDocument>();
                        var init      = false;
                        foreach (var model in models)
                        {
                            var doc = model.ModelPath.EndsWith(@"zip")
                                ? SvfDocument.LoadFromZipFile(model.ModelPath)
                                : SvfDocument.LoadFromSvfFile(model.ModelPath);
                            doc.Model.Name = model.ModelTitle;

                            if (!init)
                            {
                                targetDoc.Metadata = doc.Metadata.Clone();
                                init = true;
                            }

                            if (targetDoc.Metadata.DefaultCamera == null && doc.Metadata.DefaultCamera != null)
                            {
                                targetDoc.Metadata.DefaultCamera = doc.Metadata.DefaultCamera.Clone();
                            }

                            var transform = Metadata
                                            .GetUnitScaleTransform(doc.Metadata.Units, targetDoc.Metadata.Units)
                                            .Multiply(doc.Metadata.RefPointTransform);

                            targetDoc.Model.Children.ImportNode(doc.Model, transform);

                            doc.Reset();
                            docs.Add(doc);
                        }

                        if (targetDoc.Metadata.DefaultCamera != null)
                        {
                            targetDoc.Metadata.DefaultCamera.AutoFit = true;
                        }

                        targetDoc.SaveToFolder(txtOutput.Text, true);
                        targetDoc.Dispose();

                        foreach (var doc in docs)
                        {
                            doc.Dispose();
                        }
                        docs.Clear();

                        ProgressHelper.Close();
                        var message = $@"Merge Success! (duration: {sw.Elapsed})";
                        MessageBox.Show(this, message, Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        ProgressHelper.Close();
                        MessageBox.Show(this, ex.ToString(), Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }
Пример #2
0
        private void btnMerge_Click(object sender, EventArgs e)
        {
            var models = new List <SvfModel>();

            foreach (SvfModel item in svfModelBindingSource)
            {
                if (item == null)
                {
                    continue;
                }
                models.Add(item);
            }
            if (models.Count < 1)
            {
                MessageBox.Show(this, @"The source model list is empty!", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (string.IsNullOrEmpty(txtOutput.Text))
            {
                MessageBox.Show(this, @"Please Select Output Path!", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            var mode = (int)((cbPositioningMode.SelectedItem as ItemValue <PositioningMode>)?.Value ?? PositioningMode.BySharedCoordinates);

            using (var session = LicenseConfig.Create())
            {
                if (session.IsValid == false)
                {
                    LicenseConfig.ShowDialog(session, this);
                    return;
                }

                using (new ProgressHelper(this, @"Merging ..."))
                {
                    try
                    {
                        var sw        = Stopwatch.StartNew();
                        var targetDoc = new SvfDocument();
                        var docs      = new List <SvfDocument>();
                        var init      = false;
                        foreach (var model in models)
                        {
                            var doc = model.ModelPath.EndsWith(@"zip")
                                ? SvfDocument.LoadFromZipFile(model.ModelPath)
                                : SvfDocument.LoadFromSvfFile(model.ModelPath);
                            doc.Model.Name = model.ModelTitle;

                            if (!init)
                            {
                                targetDoc.Metadata = doc.Metadata.Clone();
                                init = true;
                            }

                            if (targetDoc.Metadata.DefaultCamera == null && doc.Metadata.DefaultCamera != null)
                            {
                                targetDoc.Metadata.DefaultCamera = doc.Metadata.DefaultCamera.Clone();
                            }

                            Transform transform;
                            switch (mode)
                            {
                            case 1:     //原点对原点
                            {
                                transform = Metadata
                                            .GetUnitScaleTransform(doc.Metadata.Units, targetDoc.Metadata.Units);
                                break;
                            }

                            case 2:     //项目基点对项目基点
                            {
                                transform = Metadata
                                            .GetUnitScaleTransform(doc.Metadata.Units, targetDoc.Metadata.Units)
                                            .Multiply(doc.Metadata.PrjPointTransform);
                                break;
                            }

                            case 3:     //中心对中心
                            {
                                var box = doc.Metadata.WorldBoundingBox;
                                if (box == null || box.Empty())
                                {
                                    transform = Metadata
                                                .GetUnitScaleTransform(doc.Metadata.Units, targetDoc.Metadata.Units);
                                }
                                else
                                {
                                    var offset = new Vector3D();
                                    offset.X  = -(box.Max.X + box.Min.X) / 2;
                                    offset.Y  = -(box.Max.Y + box.Min.Y) / 2;
                                    offset.Z  = -(box.Max.Z + box.Min.Z) / 2;
                                    transform = Metadata
                                                .GetUnitScaleTransform(doc.Metadata.Units, targetDoc.Metadata.Units)
                                                .Multiply(Transform.GetTranslation(offset));
                                }
                                break;
                            }

                            case 0:     //通过共享坐标
                            default:
                            {
                                transform = Metadata
                                            .GetUnitScaleTransform(doc.Metadata.Units, targetDoc.Metadata.Units)
                                            .Multiply(doc.Metadata.RefPointTransform);
                                break;
                            }
                            }

                            targetDoc.Model.Children.ImportNode(doc.Model, transform);

                            doc.Reset();
                            docs.Add(doc);
                        }

                        if (targetDoc.Metadata.DefaultCamera != null)
                        {
                            targetDoc.Metadata.DefaultCamera.AutoFit = true;
                        }

                        targetDoc.SaveToFolder(txtOutput.Text, true);
                        targetDoc.Dispose();

                        foreach (var doc in docs)
                        {
                            doc.Dispose();
                        }
                        docs.Clear();

                        ProgressHelper.Close();
                        var message = $@"Merge Success! (duration: {sw.Elapsed})";
                        MessageBox.Show(this, message, Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        ProgressHelper.Close();
                        MessageBox.Show(this, ex.ToString(), Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }